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

Letter Grade

Turn a test score into a letter grade.

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

  • Map score ranges to grades
  • Order conditions from highest to lowest
  • Handle boundary values exactly

Đề bài

Xem bản dịch

Convert a score from 0 to 100 into a letter grade: 90 and above is A, 80–89 is B, 70–79 is C, 60–69 is D, and below 60 is F.

Implement letter_grade(score).

Input

score: The test score.

Output

Return the letter as a one-character string.

Ràng buộc

0 ≤ score ≤ 100

Ví dụ

Ví dụ 1
Đầu vào:
score = 85

Kết quả mong đợi:
expected = "B"
Đầu vào:
long long score = 85;

Kết quả mong đợi:
std::string expected = "B";
Đầu vào:
long score = 85;

Kết quả mong đợi:
string expected = "B";

Giải thích

85 is at least 80 but below 90, so the grade is B.

Ví dụ 2
Đầu vào:
score = 59

Kết quả mong đợi:
expected = "F"
Đầu vào:
long long score = 59;

Kết quả mong đợi:
std::string expected = "F";
Đầu vào:
long score = 59;

Kết quả mong đợi:
string expected = "F";

Giải thích

59 is below 60, so the grade is F.

Hướng tiếp cận

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

Gợi ý 1

Compare the score against the highest passing threshold first and work downward.

Gợi ý 2

A chain of ordered comparisons is the simplest way to map ranges to letters.

Gợi ý 3

Make sure boundary scores like 90, 80, 70, and 60 fall into the higher grade band.

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

O(1) time, O(1) 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.

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.