예약 모듈 포팅하기
Coddy의 GDScript 개발자를 위한 C 코스 레슨. 48개 중 48번째.
챌린지
중급remaining 필드와 room_reserve(Room*,request)를 사용하는 Room 구조체로 GDScript 예약 알고리즘을 포팅하세요. 수용 인원은 음이 아닌 정수이고, 요청은 정수입니다. 수용 가능하고 양수인 요청만 수락하여 true를 반환하고 remaining을 감소시키세요. 거부할 때는 값을 변경하지 마세요. 입력은 수용 인원 다음에 요청들이 주어지며, remaining:accepted를 출력하세요. 잠긴 main은 두 방과 실패한 업데이트를 각각 독립적으로 검사합니다. 이는 구조체와 함수로 이루어진 인터페이스이지, C 클래스가 아닙니다.
GDScript 참조
extends Node
func _ready():
var tokens = PackedStringArray()
var line = OS.read_string_from_stdin()
while not line.is_empty():
tokens.append_array(line.split(" ", false))
line = OS.read_string_from_stdin()
var values: Array[int] = []
for token in tokens:
values.append(int(token))
var capacity = values[0]
var remaining = capacity
var accepted = 0
for request in values.slice(1):
if request > 0 and request <= remaining:
remaining -= request
accepted += 1
print("%d:%d" % [remaining, accepted])잠금 해제된 소스 파일과 헤더 파일을 수정하세요. 이를 직접 호출하는 공개된 잠금 상태의 main.c와 선언된 공개 API의 호환성을 유지하세요. main을 추가하지 마세요. 작업에서 범위를 명시적으로 검사하는 경우가 아니라면, 모든 숫자 입력 테스트 데이터는 오버플로를 피할 수 있을 만큼 작습니다. 지정된 출력만 출력하세요. 드라이버가 표시된 경우 입력 파싱과 출력을 처리합니다.
직접 해보기
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>
#include "lesson.h"
int main(void){Room a={3},b={5};if(room_reserve(&a,0)||room_reserve(&a,-1)||room_reserve(&a,4)||a.remaining!=3||!room_reserve(&a,2)||a.remaining!=1||b.remaining!=5)return 3;Room room;if(scanf("%d",&room.remaining)!=1)return 1;int request,accepted=0;while(scanf("%d",&request)==1)if(room_reserve(&room,request))accepted++;printf("%d:%d\n",room.remaining,accepted);return 0;}
GDScript 개발자를 위한 C의 모든 레슨
직접 연습해 보세요: 온라인 C 컴파일러