PAT 1038 統計同成績學生 (20分)(Java)

題目描述

  本題要求讀入 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

代碼

package com.hbut.pat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
 
public class Pat_1038{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        String inputString = br.readLine();
 
        String[] eachScore = inputString.split("\\s+");
        String[] outScore = br.readLine().split("\\s+");
 
 
        int[] scoreCounts = new int[120];
        for (int i=0; i<eachScore.length; ++i) {
            int score = Integer.parseInt(eachScore[i]);
            scoreCounts[score]++;
        }
        boolean tag = true;
        for (int i=1; i<outScore.length; ++i) {
            int score = Integer.parseInt(outScore[i]);
            if (tag) {
                System.out.print(scoreCounts[score]);
                tag = false;
            } else {
                System.out.print(" "+scoreCounts[score]);
            }
        }
        System.out.println();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章