Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Temperature Table

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

−500 ≤ start, stop ≤ 1,000

1 ≤ step ≤ 100

Ví dụ

Ví dụ 1
Đầu vào:
start = 32
stop = 72
step = 20

Kết quả mong đợi:
expected = ["32 F = 0.0 C", "52 F = 11.1 C", "72 F = 22.2 C"]
Đầu vào:
long long start = 32;
long long stop = 72;
long long step = 20;

Kết quả mong đợi:
std::vector<std::string> expected = {"32 F = 0.0 C", "52 F = 11.1 C", "72 F = 22.2 C"};
Đầu vào:
long start = 32;
long stop = 72;
long step = 20;

Kết quả mong đợi:
string[] expected = new string[] {"32 F = 0.0 C", "52 F = 11.1 C", "72 F = 22.2 C"};

Giải thích

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.

Ví dụ 2
Đầu vào:
start = 0
stop = 35
step = 31

Kết quả mong đợi:
expected = ["0 F = -17.8 C", "31 F = -0.6 C"]
Đầu vào:
long long start = 0;
long long stop = 35;
long long step = 31;

Kết quả mong đợi:
std::vector<std::string> expected = {"0 F = -17.8 C", "31 F = -0.6 C"};
Đầu vào:
long start = 0;
long stop = 35;
long step = 31;

Kết quả mong đợi:
string[] expected = new string[] {"0 F = -17.8 C", "31 F = -0.6 C"};

Giải thích

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.

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

Round the absolute value, then put the sign back.

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.