समस्या
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.
सीमाएँ
0 ≤ score ≤ 100
उदाहरण
इनपुट:
score = 85
अपेक्षित आउटपुट:
expected = "B"
इनपुट:
long long score = 85;
अपेक्षित आउटपुट:
std::string expected = "B";
इनपुट:
long score = 85;
अपेक्षित आउटपुट:
string expected = "B";
व्याख्या
85 is at least 80 but below 90, so the grade is B.
इनपुट:
score = 59
अपेक्षित आउटपुट:
expected = "F"
इनपुट:
long long score = 59;
अपेक्षित आउटपुट:
std::string expected = "F";
इनपुट:
long score = 59;
अपेक्षित आउटपुट:
string expected = "F";
व्याख्या
59 is below 60, so the grade is F.
कैसे हल करें
पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।
संकेत 1
Score को सबसे ऊँचे passing threshold से compare करो और नीचे की तरफ़ जाओ.
संकेत 2
Ranges को letters में map करने के लिए ordered comparisons की chain सबसे सरल है.
संकेत 3
90, 80, 70, और 60 जैसे boundary scores higher grade band में आने चाहिए.
अपेक्षित जटिलता
समय O(1), अतिरिक्त स्थान O(1)
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।