WPGGJ 2022-01-09 15:14 采纳率: 47.4%
浏览 49

java统计同成绩学生,想知道部分代码意思


```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
    public static void main(String[] args) throws Exception{
        int []score=new int[101];
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        try {
            int n= Integer.parseInt(in.readLine());
            String[] inp=in.readLine().split(" ");
            for (int i=0;i<n;i++){
                score[Integer.parseInt(inp[i])]++;
            }
            String[] s=in.readLine().split(" ");
            int k=Integer.parseInt(s[0]);
            for (int i=0;i<k-1;i++){
                System.out.print(score[Integer.parseInt(s[i+1])]+" ");
            }
            System.out.print(score[Integer.parseInt(s[s.length-1])]);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



想具体知道这一块的意思,有点看不懂

```java

 for (int i=0;i<n;i++){
                score[Integer.parseInt(inp[i])]++;
            }
            String[] s=in.readLine().split(" ");
            int k=Integer.parseInt(s[0]);
            for (int i=0;i<k-1;i++){
                System.out.print(score[Integer.parseInt(s[i+1])]+" ");
            }
            System.out.print(score[Integer.parseInt(s[s.length-1])]);
  • 写回答

1条回答 默认 最新

  • zlebhs 2022-01-13 16:40
    关注
    
        public static void main(String[] args) {
            int[] score = new int[101];
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            try {
                // n 输入的个数
                int n = Integer.parseInt(in.readLine());
                // 输入成绩,个数必须 >= n
                String[] inp = in.readLine().split(" ");
                // int[] score = new int[101]; 看这个数组的长度,101,对应索引为0-100
                // 那么你的分数只要在0-100之间,必定能在该数组中找到唯一的位置,那么该数组每个索引(分数)对应的值就可以代表人数
                for (int i = 0; i < n; i++) {
                    // 看不懂下面的那个简写可以拆开来看,如下
                    // 拿到此次循环的分数
                    int cur = Integer.parseInt(inp[i]);
                    // 该分数人数 + 1
                    score[cur] = score[cur] + 1;
                    // score[Integer.parseInt(inp[i])]++;
                }
                // 从下面的代码来看,这个输入的形式为   k, score1, score2,...,scorek
                // k 就是查询的个数,score为要查询的分数,至少要为k个
                String[] s = in.readLine().split(" ");
                int k = Integer.parseInt(s[0]);
                for (int i = 0; i < k - 1; i++) {
                    // 找到对应的分数,同理这里不懂也可以拆开来看
                    // 拿到此次循环的分数
                    int cur = Integer.parseInt(s[i + 1]);
                    // 拿到总人数
                    int sum = score[Integer.parseInt(s[i + 1])];
                    // 输出即可
                    System.out.print(sum + " ");
                    // System.out.print(score[Integer.parseInt(s[i + 1])] + " ");
                }
                // 上面的循环少处理了最后一个数,在这边输出了
                // 不过我不知道为什么他上面的循环要少输出一个数
                // 可以把这句去掉,上面循环条件改成 i < k
                System.out.print(score[Integer.parseInt(s[s.length - 1])]);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 1月9日

悬赏问题

  • ¥15 CPU卡指令整合指令数据都在图片上
  • ¥15 火车票关联12306问题
  • ¥15 odoo17处理受托加工产品
  • ¥15 如何用MATLAB编码图三的积分
  • ¥15 圆孔衍射光强随孔径变化
  • ¥15 MacBook pro m3max上用vscode运行c语言没有反应
  • ¥15 ESP-PROG配置错误,ALL ONES
  • ¥15 结构功能耦合指标计算
  • ¥50 AI大模型精调(百度千帆、飞浆)
  • ¥15 非科班怎么跑代码?如何导数据和调参