1038 统计同成绩学生

一、题目:

本题要求读入N名学生的成绩,将获得某一给定分数的学生人数输出。

输入格式:

输入在第1行给出不超过10^5^的正整数N,即学生总人数。随后1行给出N名学生的百分制整数成绩,中间以空格分隔。最后1行给出要查询的分数个数K(不超过N的正整数),随后是K个分数,中间以空格分隔。

输出格式:

在一行中按查询顺序给出得分等于指定分数的学生人数,中间以空格分隔,但行末不得有多余空格。

输入样例:

10
60 75 90 55 75 99 82 90 75 50
3 75 90 88

输出样例:

3 2 0

二、题解:
因为分数在0到100之间,所以直接开一个数组用于存放分数的个数即可。用C++语言实现会超时,而C语言版本完美通过,我也没弄明白咋回事。


三、代码:
(C++版运行超时):
 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main(){
 5     int b[101]={0};
 6     int n,m;
 7     cin>>n;
 8     for(int i=0;i<n;i++){
 9        int tmp=0;
10        cin>>tmp;
11        b[tmp]++;
12     }
13     cin>>m;
14     for(int i=0;i<m;i++){
15         int cnt;
16         cin>>cnt;
17         cout<<b[cnt];
18         if(i!=m-1) cout<<" ";
19     }
20     return 0;
21 }

(C语言版答案正确):

 1 #include<stdio.h>
 2 
 3 int main(){
 4   int b[101]={0};
 5     int n,m;
 6     scanf("%d",&n);
 7     for(int i=0;i<n;i++){
 8        int tmp=0;
 9        scanf("%d",&tmp);
10        b[tmp]++;
11     }
12     scanf("%d",&m);
13     for(int i=0;i<m;i++){
14         int cnt;
15         scanf("%d",&cnt);
16         printf("%d",b[cnt]);
17         if(i!=m-1) printf(" ");
18     }
19     return 0;
20 }

猜你喜欢

转载自www.cnblogs.com/Gzu_zb/p/9381673.html