Menu
CoddyTech

복습: 객실 예약

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

challenge icon

챌린지

쉬움

음수가 아닌 용량, 비공개 remaining 상태, 읽을 수 있는 Remaining 및 Reserve(int)를 갖춘 Room을 구현하세요. 용량에 맞는 양수 요청만 허용하세요. 실패한 요청은 상태를 변경하지 않습니다. yes/no 결과를 쉼표와 콜론으로 연결하고 남은 용량을 반환하세요. 드라이버는 클래스를 독립적으로 테스트합니다.

잠금 해제된 C# 파일을 수정하세요. 보이는 잠금된 Main.cs는 설명된 줄을 읽고, Exercise.Solve를 호출하며, 입력이 변경되지 않았는지 확인하고 결과를 마지막 줄바꿈 하나와 함께 출력합니다. 선언된 API를 유지하세요. 두 번째 Main을 추가하거나 진단 출력을 인쇄하지 마세요.

잠금된 실행기도 Solve 래퍼뿐 아니라 내보낸 도우미 또는 클래스 API를 독립적으로 테스트합니다.

직접 해보기

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
public class Program {
static int I(string text) { return int.Parse(text,CultureInfo.InvariantCulture); }
static int[] Ints(string text) { return text.Length==0 ? new int[0] : text.Split(',').Select(I).ToArray(); }
static string[] Words(string text) { return text.Length==0 ? new string[0] : text.Split(','); }
static string Snapshot(object value) { if (value==null) return "<null>"; if (value is string) return (string)value; var sequence=value as System.Collections.IEnumerable; if (sequence!=null) { var items=new List<string>(); foreach (object item in sequence) items.Add(Snapshot(item)); return string.Join("|",items); } return Convert.ToString(value,CultureInfo.InvariantCulture); }
public static void Main() {
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var rows = new List<string>(); string line; while ((line=Console.ReadLine()) != null) rows.Add(line);
string[] input=rows.ToArray();
var a0 = I(input[0]); string before0=Snapshot(a0);
var a1 = Ints(input[1]); string before1=Snapshot(a1);
var r1 = new Room(3); var r2 = new Room(8); if (r1.Reserve(0) || r1.Reserve(-1) || r1.Reserve(4) || r1.Remaining != 3 || !r1.Reserve(2) || r1.Remaining != 1 || r2.Remaining != 8) throw new Exception("Room API");
var result = Exercise.Solve(a0,a1);
if (Snapshot(a0) != before0) throw new Exception("Input mutation");
if (Snapshot(a1) != before1) throw new Exception("Input mutation");
Console.WriteLine(result);
}
}

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

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