Menu
CoddyTech

복습: 배송 정책

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

challenge icon

챌린지

쉬움

별도의 파일에서 두 가지 IShipping 구현을 완성하세요. Standard는 0 이상의 무게에 2를 곱한 비용을 부과합니다. Express는 Standard 비용에 5를 더합니다. 플래그에 따라 선택하고 인터페이스를 통해 호출하도록 Solve를 구현하세요. 직접 실행하는 드라이버의 테스트도 통과해야 합니다.

잠금 해제된 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 = input[1] == "true"; string before1=Snapshot(a1);
IShipping ship1 = new StandardShipping(); IShipping ship2 = new ExpressShipping(); if (ship1.Cost(4) != 8 || ship2.Cost(4) != 13 || ship2.Cost(0) != 5) throw new Exception("Shipping 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);
}
}

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

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