최종: 계정 이전
Coddy의 C# 개발자를 위한 GDScript 코스 레슨. 48개 중 48번째.
챌린지
쉬움balance를 가진 FundsAccount(initial:int)를 구현하세요. static FundsTransfer.move(source:FundsAccount,target:FundsAccount,n:int)->bool를 구현하세요. null 계정, 음수 금액 또는 출금 계정의 잔액 부족은 변경 없이 거부하세요. 그 외에는 한 번 이체하세요. 자기 자신에게 이체하는 경우 금액이 충분하면 성공하고 잔액은 변하지 않습니다. Solve는 [a,b,n]을 받아 success:a:b를 반환합니다. 초기 잔액은 음수가 아닙니다.
입력은 stdin으로 전달되는 하나의 JSON 배열입니다. 잠긴 main은 이를 파싱하고, 공개 API를 호출한 뒤 반환된 텍스트 앞에 result=를 붙여 개행과 함께 출력합니다. 보조 함수는 출력하면 안 됩니다. 입력 사례를 처리하기 전에 독립적인 고정 프로브로 필수 API 동작을 검증합니다. 프로브는 구현 방식을 강제하지 않습니다.
완성된 C# 참조 구현입니다. 나열된 파일을 System.Web.Extensions 프레임워크 어셈블리와 함께 컴파일하세요. Program.Main은 동일한 JSON 배열을 읽고 동일한 결과 프로토콜로 출력합니다. 이는 독립 실행형 C# 참조 구현이며, Godot C# 통합이 아닙니다.
C# 파일 Account.cs:
public class Account {
public long Balance;
public Account(long initial = 0) { Balance = initial; }
}C# 파일 Transfers.cs:
public static class Transfers {
public static bool Move(Account source, Account target, long amount) {
if (source == null || target == null || amount < 0 || amount > source.Balance)
return false;
source.Balance -= amount;
target.Balance += amount;
return true;
}
}C# 파일 Program.cs:
using System;
using System.Web.Script.Serialization;
public static class Program {
public static void Main(string[] args) {
if (args.Length > 0 && args[0] == "--probe") {
var a = new Account(6);
if (Transfers.Move(null,a,0) || Transfers.Move(a,null,0) ||
!Transfers.Move(a,a,4) || a.Balance != 6 ||
Transfers.Move(a,a,7) || a.Balance != 6)
throw new Exception("contract failure");
Console.WriteLine("contract-ok");
return;
}
var values = new JavaScriptSerializer().Deserialize<long[]>(Console.ReadLine());
var source = new Account(values[0]);
var target = new Account(values[1]);
bool moved = Transfers.Move(source,target,values[2]);
Console.WriteLine("result=" + (moved ? "true" : "false") + ":" + source.Balance + ":" + target.Balance);
}
}코드는 funds_account.gd, funds_transfer.gd, exercise.gd에 작성하세요. main.gd에는 제공된 입력/출력 코드가 있으며 수정할 수 없습니다. 각 class_name 유형은 이름이 지정된 파일에 두세요.
직접 해보기
extends Node
func contract_ok(data: Array) -> bool:
var a: FundsAccount = FundsAccount.new(6)
var b: FundsAccount = FundsAccount.new(2)
if FundsTransfer.move(null,b,0) or FundsTransfer.move(a,null,0) or FundsTransfer.move(a,b,7) or FundsTransfer.move(a,b,-1): return false
if a.balance != 6 or b.balance != 2: return false
if not FundsTransfer.move(a,a,4) or a.balance != 6: return false
if not FundsTransfer.move(a,b,3) or a.balance != 3 or b.balance != 5: return false
return true
func _ready() -> void:
var data: Array = JSON.parse_string(OS.read_string_from_stdin())
if not contract_ok(data):
print("API_CONTRACT_FAILED")
return
print("result=" + Exercise.solve(data))
C# 개발자를 위한 GDScript의 모든 레슨
직접 연습해 보세요: 온라인 GDScript 컴파일러