10370 - Above Average
It is said that 90% of frosh expect to be above average in their class. You are to provide a reality check.
The first line of standard input contains an integer C, the number of test cases. C data sets follow. Each data set begins with an integer, N, the number of people in the class (1 <= N <= 1000). N integers follow, separated by spaces or newlines, each giving the final grade (an integer between 0 and 100) of a student in the class. For each case you are to output a line giving the percentage of students whose grade is above average, rounded to 3 decimal places.
据说,90%的大学一年级新生期望他们自己的成绩能在全班平均之上,请你来帮忙验证看看他们有没有达成目标。
Input
输入的第一列有一个整数C 代表以下有多少组测试资料。每组资料的第一个整数N 代表班级总人数( 1 <= N <= 1000 )。接下来有N个以空白或换行来间隔的整数,代表每个学生的期末总成绩( 0 <= 分数<= 100 )。
Output
对每组测试资料输出一列,算出有多少百分比的学生成绩比全班平均高,请四舍五入到小数第三位。
Sample Input
5 5 50 50 70 80 100 7 100 95 90 80 70 60 50 3 70 90 80 3 70 90 81 9 100 99 98 97 96 95 94 93 91
Output for Sample Input
40.000% 57.143% 33.333% 66.667% 55.556%
解题思路:按照题目给定的要求,先求平均数再算比例,这体制要个小数有关的就定义为double
#include<stdio.h> int main() {int n,s,a[1000]={0},i,b; double sum,t; scanf("%d",&n); while(n--){t=0; scanf("%d",&s); for(sum=i=0;i<s;i++){ scanf("%d",&a[i]); sum+=a[i]; } sum/=s; for(t=i=0;i<s;i++) if(a[i]>sum)t++; printf("%.3f%%\n",t/s*100); } return 0; }