本题要求读入 N 名学生的成绩,将获得某一给定分数的学生人数输出。
输入格式:
输入在第 1 行给出不超过 10
5
的正整数 N,即学生总人数。随后一行给出 N 名学生的百分制整数成绩,中间以空格分隔。最后一行给出要查询的分数个数 K(不超过 N 的正整数),随后是 K 个分数,中间以空格分隔。
输出格式:
在一行中按查询顺序给出得分等于指定分数的学生人数,中间以空格分隔,但行末不得有多余空格。
输入样例:
10
60 75 90 55 75 99 82 90 75 50
3 75 90 88
输出样例:
3 2 0
解题思路
这题我感觉用Java做应该是必定超时了,目前还没找到解决办法。
看了下别人的C++,最后一个测试点30ms,前面几个测试点4ms,那么最后最后一个点就是前面的7倍多。
用JAVA你会发现,再快前面3个测试点也只能在60ms左右,按照推论那么最后一个测试点应该是在450ms左右。
而题目规定的250ms,所以此题用java解决的话你必须保证前面几个测试点在30ms,而这我目前还没看到过用Java能达到这个速度的。
题目简单,看代码吧。
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder("");
int[] score = new int[101];
int m = Integer.parseInt(br.readLine()); // 学生人数
String[] str = br.readLine().split(" "); // 每名学生的成绩
String[] cx = br.readLine().split(" "); // 要查询的人数 和成绩
br.close();
for(int i = 0 ; i< m ; i++) {
score[Integer.parseInt(str[i])]++;
}
int cxshu = Integer.parseInt(cx[0]); //要查询的人数
for (int i = 0; i < cxshu; i++) {
if (i != cxshu-1) {
sb.append(score[Integer.parseInt(cx[i+1])] + " ");
}else {
sb.append(score[Integer.parseInt(cx[i+1])]);
}
}
out.print(sb);
out.flush();
}
}
附上别人的C++,最后一个未超时。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int num, get, find, grade[101] = {0}, n, recover;
cin >> num;
for(int i = 0; i < num; i++)
{
scanf("%d",&get);
if(get >= 0 && get <= 100)
grade[get] ++;
}
scanf("%d",&n);
for(int i = 0; i < n; i++)
{
scanf("%d",&find);
if(i) printf(" %d",grade[find]);
else printf("%d",grade[find]);
}
return 0;
}