码迷,mamicode.com
首页 > 编程语言 > 详细

C语言 · 分数统计

时间:2017-02-26 12:40:12      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:时间   stream   tns   else   文件中   提高   enter   scanf   提交   

算法提高 分数统计  
时间限制:1.0s   内存限制:512.0MB
    
问题描述
  2016.4.5已更新此题,此前的程序需要重新提交。
问题描述
  给定一个百分制成绩T,将其划分为如下五个等级之一:
  90~100为A,80~89为B,70~79为C,60~69为D,0~59为E
  现在给定一个文件inp,文件中包含若干百分制成绩(成绩个数不超过100),请你统计五个等级段的人数,并找出人数最多的那个等级段,按照从大到小的顺序输出该段中所有人成绩(保证人数最多的等级只有一个)。要求输出到指定文件oup中。
输入格式
  若干0~100的正整数,用空格隔开
输出格式
  第一行为5个正整数,分别表示A,B,C,D,E五个等级段的人数
  第二行一个正整数,表示人数最多的等级段中人数
  接下来一行若干个用空格隔开的正整数,表示人数最多的那个等级中所有人的分数,按从大到小的顺序输出。
样例输入
100 80 85 77 55 61 82 90 71 60
样例输出
2 3 2 2 1
3
85 82 80
 
作者注释:蓝桥杯的后台编译时其实有N——表输入N个0~100的正整数即分数。
正确代码:
 1 #include <algorithm>  
 2 #include <iostream>  
 3 #include <string.h>  
 4 int num[1005];  
 5 using namespace std;  
 6 int main()  
 7 {  
 8     int n,i = 0,sum;  
 9     int temp[5][1005],dp[5] = {90,80,70,60,0};  
10     memset(temp,0,sizeof(temp));  
11     cin>>sum;  
12     for(;i < sum;i++)  
13     {  
14         cin>>num[i];  
15         for(int j = 0;j < 5;j++)  
16         {  
17             if(num[i] >= dp[j])  
18             {  
19                 temp[j][++temp[j][0]] = num[i];  
20                 break; 
21             }  
22         }  
23     }  
24     int maxn = 0,maxl = 0;  
25     for(int t = 0;t < 5;t++)  
26     {  
27         cout<<temp[t][0]<<" ";//输出每个等级的人数 
28         if(maxn < temp[t][0])  
29         {  
30             maxn = temp[t][0];  
31             maxl = t;  
32         }  
33     }  
34     cout<<endl<<maxn<<endl;  
35     sort(temp[maxl]+1,temp[maxl]+temp[maxl][0]+1);  
36     for(int t = temp[maxl][0];t >= 1;t--)  
37         cout<<temp[maxl][t]<<" ";  
38     return 0;  
39 } 
 
错误代码:
 1 #include<stdio.h>
 2 int main(){
 3     int n;
 4     int a=1,b=1,c=1,d=1,e=1;
 5     scanf("%d",&n);
 6     int score[n];
 7     int a1[5][n];
 8     for(int i=0;i<5;i++){
 9         a1[i][0]=0;
10     }
11     for(int i=0;i<n;i++){
12         scanf("%d",&score[i]);
13         if(score[i]>=90){
14             a1[0][a]=score[i]; 
15             a1[0][0]++;
16             a++;
17         }
18         else if(score[i]>=80 && score[i]<90){
19             a1[1][b]=score[i];
20             a1[1][0]++; 
21             b++;
22         }
23         else if(score[i]>=70 && score[i]<80){
24             a1[2][c]=score[i]; 
25             a1[2][0]++;
26             c++;
27         }
28         else if(score[i]>=60 && score[i]<70){
29             a1[3][d]=score[i];
30             a1[3][0]++; 
31             d++;
32         }
33         else if(score[i]<60){
34             a1[4][e]=score[i];
35             a1[4][0]++;
36             e++;
37         }
38     }
39     printf("%d %d %d %d %d\n",a1[0][0],a1[1][0],a1[2][0],a1[3][0],a1[4][0]);
40     
41     /*对各个等级人数进行冒泡降序*/
42     for(int i=0; i<5-1; i++){
43         for(int j=0; j<5-1-i; j++){
44             /*降序*/
45             if(a1[j][0]<a1[j+1][0]){
46                 int t = a1[j][0];
47                 a1[j][0] = a1[j+1][0];
48                 a1[j+1][0] = t;
49             }
50         }
51     }
52     printf("%d\n",a1[0][0]);
53     for(int i=1;i<=a1[0][0];i++){
54         printf("%d ",a1[1][i]);
55     }
56 }

 

 

C语言 · 分数统计

标签:时间   stream   tns   else   文件中   提高   enter   scanf   提交   

原文地址:http://www.cnblogs.com/panweiwei/p/6443834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!