Exercise 3-3 of Zhejiang University Edition "C Language Programming (3rd Edition)"

Exercise 3-3 Count the average grade and the number of passing students (15 points)

This question requires writing a program to calculate the average grade of the students and count the number of passers (with a grade of not less than 60 points). The question ensures that the input and output are within the integer range.
Input format:
Input a non-negative integer N in the first line, which is the number of students. The second line gives N non-negative integers, that is, the scores of these N students, separated by spaces.
Output format: output
according to the following format:
average = average score
count = number of passers
The average is accurate to one decimal place.
Input sample:
5
77 54 92 73 60
Output sample:
average = 71.2
count = 4
Author
C course group
Unit
Zhejiang University
Code length limit

16 KB
Time limit
400 ms
Memory limit
64 MB

#include <stdio.h>

int main() {
    
    
    int a, sum = 0, b, count = 0;
    if (scanf("%d", &a) == 1) {
    
    
        if (a > 0) {
    
    
            for (int i = 1; i <= a; i++) {
    
    
                if (scanf("%d", &b) == 1) {
    
    
                    sum += b;
                    if (b >= 60) {
    
    
                        count++;
                    }
                }
            }
            printf("average = %.1f\ncount = %d", (double) sum / a, count);
        } else {
    
    
            printf("average = 0.0\ncount = 0");
        }

    }
    return 0;
}

Guess you like

Origin blog.csdn.net/DoMoreSpeakLess/article/details/109348228