开一个Count数组,然后不断读入就好了,望采纳
#include<stdio.h>
#include<math.h>
int Count[10];
void slove(){
int n = 0;
int k;
while(scanf("%d",&k) && k != -1) {
if(k < 1 || k > 5) {
printf("This is a error input!");
continue;
}
n++;
Count[k] ++;
}
printf("n = %d\n",n);
printf("Grade(*)\tCount(n)\tPercent(%)\n");
for(int i = 5;i > 0; --i) {
double Pr = (Count[i]*100.0)/(n);
printf("%d\t\t%d\t\t%lf\n",i,Count[i],Pr);
}
}
int main()
{
slove();
return 0;
}
/*
5
5
5
5
5
5
5
5
4
4
4
4
4
4
3
-1
*/