Tất cả thử thách
Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Gradebook Letter Counts

Turn a whole class's scores into letter grades and count how many students got each letter.

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

  • Map a score to a letter with ordered conditions
  • Tally results in a fixed-size array
  • Return counts in a defined order

Đề bài

Xem bản dịch

At the end of the term a teacher wants a quick summary of the class: how many students earned each letter grade. The scale is 90 and above for A, 80 to 89 for B, 70 to 79 for C, 60 to 69 for D and below 60 for F.

Implement grade_letter_counts(scores).

Input

scores: Every student's final score.

Output

Return five counts in the order A, B, C, D, F. An empty class gives [0, 0, 0, 0, 0].

Ràng buộc

0 ≤ number of scores ≤ 10,000

0 ≤ score ≤ 100

Ví dụ

Ví dụ 1
Đầu vào:
scores = [95, 82, 67, 88, 45, 90]

Kết quả mong đợi:
expected = [2, 2, 0, 1, 1]
Đầu vào:
std::vector<long long> scores = {95, 82, 67, 88, 45, 90};

Kết quả mong đợi:
std::vector<long long> expected = {2, 2, 0, 1, 1};
Đầu vào:
long[] scores = new long[] {95, 82, 67, 88, 45, 90};

Kết quả mong đợi:
long[] expected = new long[] {2, 2, 0, 1, 1};

Giải thích

95 and 90 are A, 82 and 88 are B, 67 is D and 45 is F: [2, 2, 0, 1, 1].

Ví dụ 2
Đầu vào:
scores = [79, 80, 59, 60]

Kết quả mong đợi:
expected = [0, 1, 1, 1, 1]
Đầu vào:
std::vector<long long> scores = {79, 80, 59, 60};

Kết quả mong đợi:
std::vector<long long> expected = {0, 1, 1, 1, 1};
Đầu vào:
long[] scores = new long[] {79, 80, 59, 60};

Kết quả mong đợi:
long[] expected = new long[] {0, 1, 1, 1, 1};

Giải thích

Each score sits on a cutoff: 79 is C, 80 is B, 59 is F and 60 is D: [0, 1, 1, 1, 1].

Hướng tiếp cận

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

Gợi ý 1

Check the highest cutoff first so each score falls into exactly one branch.

Gợi ý 2

Use an array of five counters, one per letter.

Gợi ý 3

An empty class just returns the counters untouched.

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

O(n) 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.

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.