최종: 계정 이체
Coddy의 C 개발자를 위한 GDScript 코스 레슨. 48개 중 48번째.
챌린지
쉬움balance를 가진 FundsAccount(initial:int)를 구현하세요. 정적 FundsTransfer.move(source:FundsAccount,target:FundsAccount,n:int)->bool를 구현하세요. null 계정, 음수 금액 또는 출처 계정의 잔액 부족은 상태를 변경하지 않고 거부하세요. 그 외에는 한 번 이체하세요. 이체 가능 금액이 충분하면 자기 자신에게 이체해도 성공하며 잔액은 변하지 않습니다. Solve는 [a,b,n]을 받아 success:a:b를 반환합니다. 초기 잔액은 음수가 아닙니다.
입력은 stdin으로 전달되는 JSON 배열 하나입니다. 잠긴 main은 이를 파싱하고, 공개 API를 호출한 다음 반환된 텍스트 앞에 result=를 붙여 개행과 함께 출력합니다. 헬퍼 함수는 출력하면 안 됩니다. 입력 사례를 처리하기 전에 독립적인 고정 프로브로 필수 API 동작을 확인합니다. 이 프로브는 구현 스타일을 강제하지 않습니다.
완전한 C 참조 구현입니다. 이 파일들을 한곳에 저장하고 C17 컴파일러로 main.c를 컴파일하세요. 헤더는 헬퍼 API를 제공합니다. main은 동일한 JSON 배열을 읽고 같은 결과 프로토콜에 따라 출력합니다. 외부 라이브러리는 필요하지 않습니다.
C 파일 account.h:
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <stdint.h>
struct Account { int64_t balance; };
static inline struct Account account_make(int64_t initial) {
return (struct Account){initial};
}
#endif
C 파일 transfers.h:
#ifndef TRANSFERS_H
#define TRANSFERS_H
#include <stdbool.h>
#include "account.h"
static inline bool transfer_move(struct Account *source,
struct Account *target, int64_t amount) {
if (!source || !target || amount<0 || amount>source->balance) return false;
if (source==target) return true;
/* Match the reference's unchecked 64-bit arithmetic without C overflow. */
uint64_t sum=(uint64_t)target->balance+(uint64_t)amount;
source->balance-=amount;
target->balance=sum <= INT64_MAX ? (int64_t)sum : -1-(int64_t)(UINT64_MAX-sum);
return true;
}
#endif
C 파일 main.c:
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "transfers.h"
int main(int argc, char **argv) {
if (argc>1 && strcmp(argv[1],"--probe")==0) {
struct Account a=account_make(6);
if (transfer_move(NULL,&a,0) || transfer_move(&a,NULL,0) ||
!transfer_move(&a,&a,4) || a.balance!=6 ||
transfer_move(&a,&a,7) || a.balance!=6) return 1;
puts("contract-ok"); return 0;
}
int64_t a,b,n;
if (scanf(" [ %" SCNd64 " , %" SCNd64 " , %" SCNd64 " ]",&a,&b,&n)!=3) return 1;
struct Account source=account_make(a),target=account_make(b);
bool moved=transfer_move(&source,&target,n);
printf("result=%s:%" PRId64 ":%" PRId64 "\n",
moved?"true":"false",source.balance,target.balance);
return 0;
}
코드는 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 컴파일러