HDU - 1235 统计同成绩学生人数

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_42410605/article/details/102715416

OJ地址:https://vjudge.net/problem/HDU-1235

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

Input

测试输入包含若干测试用例,每个测试用例的格式为


第1行:N
第2行:N名学生的成绩,相邻两数字用一个空格间隔。
第3行:给定分数

当读到N=0时输入结束。其中N不超过1000,成绩分数为(包含)0到100之间的一个整数。

Output

对每个测试用例,将获得给定分数的学生人数输出。

Sample Input

3
80 60 90
60
2
85 66
0
5
60 75 90 55 75
75
0

Sample Output

1
0
2

思路:

单纯的数学题,在此题中我使用了count函数进行统计相关元素出现的次数以及memset函数来初始化数组元素。这些常用的使用函数应该记得,方便使用:algorithm头文件下的常用函数之count()和count_if()

程序代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
	int n,m;
	int a[1010] = {0};
	while(scanf("%d",&n)!=EOF&&n!=0){
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		scanf("%d",&m);
		int sum = count(a,a+n,m);
		printf("%d\n",sum);
		memset(a,0,sizeof(a));	
	}
	return 0;
} 

运行结果:

猜你喜欢

转载自blog.csdn.net/qq_42410605/article/details/102715416