Menu
CoddyTech

예약 모듈 포팅하기

Coddy의 R 개발자를 위한 C 코스 레슨. 48개 중 48번째.

challenge icon

챌린지

중급

remaining 필드와 room_reserve(Room*,request)를 사용하는 Room 구조체로 R 예약 알고리즘을 포팅하세요. 수용량은 음이 아닌 정수이고 요청은 정수입니다. 양수이면서 수용 가능한 요청만 승인하고, true를 반환하며 remaining을 감소시키세요. 거부된 요청은 상태를 변경하지 마세요. 입력은 수용량 다음에 요청들이 이어지며, remaining:accepted를 출력하세요. 잠긴 main은 두 개의 방과 실패한 갱신을 별도로 테스트합니다. 이는 구조체와 함수 인터페이스이며, C 클래스가 아닙니다.

R 참조

values <- scan(file("stdin"), what = integer(), quiet = TRUE)
capacity <- values[1]
remaining <- capacity
accepted <- 0
for (request in values[-1]) {
  if (request > 0 && request <= remaining) {
    remaining <- remaining - request
    accepted <- accepted + 1
  }
}
cat(remaining, ":", accepted, "\n", sep = "")

잠금 해제된 소스 파일과 헤더 파일을 수정하세요. 공개 선언 API는 이를 직접 호출하는, 화면에 보이는 잠긴 main.c와 호환되도록 유지하세요. 다른 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;}

R 개발자를 위한 C의 모든 레슨

직접 연습해 보세요: 온라인 C 컴파일러