모든 챌린지
쉬움 0명 해결 정답률 0%

Temperature Table

Build a Fahrenheit to Celsius conversion table with one decimal place.

배우게 될 내용

  • Generate a sequence from start to stop
  • Round to one decimal place with integers
  • Format negative decimals correctly

문제

번역본 보기

A science class needs a printed conversion chart. For every Fahrenheit temperature from start to stop, stepping by step, compute Celsius = (F − 32) × 5 / 9 and round it to one decimal place (to the nearest tenth; with these inputs an exact half never happens).

Each row looks like 50 F = 10.0 C: the Fahrenheit value, F = , the Celsius value with exactly one digit after the point, and C. Negative values have a leading -, for example 0 F = -17.8 C and 31 F = -0.6 C.

Implement temperature_table(start, stop, step).

Input

start: The first Fahrenheit value.

stop: The last value allowed; it is included if the steps land on it.

step: How much to add each time.

Output

Return the rows in order: start, start + step, start + 2·step, … while the value is at most stop. If start is greater than stop, return an empty list.

제약 조건

−500 ≤ start, stop ≤ 1,000

1 ≤ step ≤ 100

예시

예시 1
입력:
start = 32
stop = 72
step = 20

예상 출력:
expected = ["32 F = 0.0 C", "52 F = 11.1 C", "72 F = 22.2 C"]
입력:
long long start = 32;
long long stop = 72;
long long step = 20;

예상 출력:
std::vector<std::string> expected = {"32 F = 0.0 C", "52 F = 11.1 C", "72 F = 22.2 C"};
입력:
long start = 32;
long stop = 72;
long step = 20;

예상 출력:
string[] expected = new string[] {"32 F = 0.0 C", "52 F = 11.1 C", "72 F = 22.2 C"};

설명

32 F is exactly 0.0 C, 52 F is 11.11… which rounds to 11.1, and 72 F is 22.22… which rounds to 22.2.

예시 2
입력:
start = 0
stop = 35
step = 31

예상 출력:
expected = ["0 F = -17.8 C", "31 F = -0.6 C"]
입력:
long long start = 0;
long long stop = 35;
long long step = 31;

예상 출력:
std::vector<std::string> expected = {"0 F = -17.8 C", "31 F = -0.6 C"};
입력:
long start = 0;
long stop = 35;
long step = 31;

예상 출력:
string[] expected = new string[] {"0 F = -17.8 C", "31 F = -0.6 C"};

설명

0 F is −17.77… which rounds to -17.8; the next value 31 F is −0.55… which rounds to -0.6; 62 is past 35, so the table stops.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

Work in tenths of a degree: (F − 32) × 50 / 9 gives Celsius × 10.

힌트 2

Round the absolute value, then put the sign back.

힌트 3

Print the tenths as whole part, a point, and the last digit; watch values like -0.6.

예상 복잡도

O(n) time, O(n) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

아직 통과한 풀이가 없습니다. 풀어서 1위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.