HDU - 1235 統計同成績學生人數

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;
} 

運行結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章