测试输入包含若干测试用例,每个测试用例的格式为 第1行:N 第2行:N名学生的成绩,相邻两数字用一个空格间隔。 第3行:给定分数 当读到N=0时输入结束。其中N不超过1000,成绩分数为(包含)0到100之间的一个整数。
对每个测试用例,将获得给定分数的学生人数输出。
3 80 60 90 60 2 85 66 0 5 60 75 90 55 75 75 0
1 0 2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int N = scan.nextInt();// 输入的人数
if (N == 0) {// 如果检测到输入的人数为0 则结束
return;
}
int[] arr = new int[N];// 保存N个人的分数
for (int i = 0; i < arr.length; i++) {
arr[i] = scan.nextInt();
}
int T = scan.nextInt();// 目标分数 输出击中目标分数的个数
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (T == arr[i]) {
count++;
}
}
System.out.println(count);
}
}
}
#include<iostream> #include<map> using namespace std; int main() { int N; while(cin>>N) { if(N==0) return 0; else { map<int,int> mp; int score,goal; for(int i=0;i<N;i++) { cin>>score; mp[score]++; } cin>>goal; map<int,int>::iterator it=mp.find(goal); cout<<it->second<<endl; } } return 0; }使用map挺简单的。
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { map.merge(scanner.nextInt(),1,Integer::sum); } System.out.println(map.getOrDefault(scanner.nextInt(),0)); } } }
#include <iostream> using namespace std; int main() { int n = 0, scores[1000] = { 0 }, score = 0, cnt = 0; while (cin>>n && 0 != n) { cnt = n; while (cnt) { cin >> scores[--cnt]; } cin >> score; while (n--) { if (score == scores[n]) ++cnt; } cout << cnt << endl; } return 0; }
#include<iostream> using namespace std; int main(){ int n; while(cin>>n){ if(n==0) break; int grade[101]={0}; int a; for(int i=0;i<n;i++){ cin>>a; grade[a]++; } int m; cin>>m; cout<<grade[m]<<endl; } return 0; }
#include <iostream> #include <map> using namespace std; const int N = 1010; int n; map<int, int> score; int main() { while (cin >> n) { if (n == 0) break; score.clear(); for (int i = 0; i < n; i++) { int x; cin >> x; score[x]++; } int grade; cin >> grade; cout << score[grade] << endl; } } // 64 位输出请用 printf("%lld")
#include <iostream> #include<unordered_map> using namespace std; int main() { int n; while(cin>>n&&n!=0){ unordered_map<int,int> student; for(int i=0;i<n;i++){ int key; cin>>key; student[key]++; } int key; cin>>key; int ans = student[key]; cout<<ans<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int gei[10000]; int main() { int n; int zushu=0; map<int,int>mp; while(cin>>n&&n!=0){ for(int i=0;i<n;i++){ int score; cin>>score; mp[score]++; } int g; cin>>g; gei[zushu++]=g; } for(int j=0;j<zushu;j++){ if(mp.find(gei[j])!=mp.end()){ cout<<mp[gei[j]]<<endl; } else{ cout<<"0"<<endl; } } }
#include <iostream> #include <map> using namespace std; int main() { int n; while (cin >> n && n != 0) { map<int, int>number; while (n--) { int score; cin >> score; //读入成绩分数 number[score]++; //分数对应人数累加 } int score; cin >> score; cout << number[score] << endl; } return 0; }
#include <cstdio> int main(){ int n; int arr[1001]; int target; while(scanf("%d",&n) != EOF){ if(n <= 0){ //do nothing }else{ //n>0 for(int i = 0; i < n; ++i){ scanf("%d",&arr[i]); } scanf("%d",&target); int num = 0; for(int i = 0; i < n; ++i){ if(arr[i] == target){ num++; } } printf("%d\n",num); } } return 0; }