蓝桥杯 Day3 java组 排序与排列

第一题 统计数字

某次科研调查时得到了 n 个自然数,每个数均不超过 (1.5×10^9 )。已知不相同的数不超过 10000 个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出统计结果。

输入描述:
第 1 行是整数 n,表示自然数的个数。
第 2∼n 行每行一个自然数。

8
2
4
2
4
5
100
2
100
输出描述:
输出 m 行(m 为 n 个自然数中不相同数的个数),按照自然数从小到大的顺序输出。每行输出两个整数,分别是自然数和该数出现的次数,其间用一个空格隔开。

2 3
4 2
5 1
100 2

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int a[] = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }
        Arrays.sort(a);

        int temp = a[0];
        int count = 1;
        int i;
        for (i = 1; i < n; i++) {

            if (temp == a[i]) {
                count++;
            }

            if (temp != a[i]) {
                System.out.println(a[i - 1] + "," + count);
                count = 1;
            }

            temp = a[i];
        }
        System.out.println(a[i - 1] + "," + count);
    }
}

第二题 错误票据

某涉密单位下发了某种票据,并要在年终全部收回。

每张票据有唯一的 ID 号。全年所有票据的 ID 号是连续的,但ID的开始数码是随机选定的。

因为工作人员疏忽,在录入 ID号的时候发生了一处错误,造成了某个 ID 断号,另外一个 ID 重号。你的任务是通过编程,找出断号的 ID 和重号的 ID。(假设断号不可能发生在最大和最小号)

输入描述
2
5 6 8 11 9
10 12 9
输出描述
7 9

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int quehao=0,duanhao=0;
        Scanner scanner = new Scanner(System.in);
        ArrayList<Integer> arrayList = new ArrayList<>();
        int n = scanner.nextInt();
        scanner.nextLine();//吃换行???
        for (int i = 0; i < n; i++) {
            String s1 = scanner.nextLine();//next()方法读取到空白符就结束l;nextLine()读取到回车结束也就是“\r”;
            String[] s2= s1.split("\\s+");将一个字符串分割为子字符串,然后将结果作为字符串数组返回。  \\s表示   空格,回车,换行等空白符  +表示一个或多个的意思
            for (int j = 0; j < s2.length; j++) {
                arrayList.add(Integer.parseInt(s2[j]));
            }
        }
        Collections.sort(arrayList);//对集合进行排序
        for (int i = 1; i < arrayList.size(); i++) {
            if(arrayList.get(i)-arrayList.get(i-1)==2){
                duanhao=arrayList.get(i)-1;
            }
            if(arrayList.get(i)-arrayList.get(i-1)==0){
                quehao=arrayList.get(i);
            }
        }
        System.out.println( quehao + " " + duanhao);
    }
}

很精彩

第三题 奖学金

某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前 5 名学生发奖学金。期末,每个学生都有 3 门课的成绩:语文、数学、英语。先按总分从高到低排序,如果两个同学总分相同,再按语文成绩从高到低排序,如果两个同学总分和语文成绩都相同,那么规定学号小的同学排在前面,这样,每个学生的排序是唯一确定的。

任务:先根据输入的 3 门课的成绩计算总分,然后按上述规则排序,最后按排名顺序输出前 5 名学生的学号和总分。注意,在前 5 名同学中,每个人的奖学金都不相同,因此,你必须严格按上述规则排序。例如,在某个正确答案中,如果前两行的输出数据(每行输出两个数:学号、总分)是:

7 279
5 279

这两行数据的含义是:总分最高的两个同学的学号依次是 7 号、5 号。这两名同学的总分都是 279 (总分等于输入的语文、数学、英语三科成绩之和),但学号为 7 的学生语文成绩更高一些。如果你的前两名的输出数据是:

5 279
7 279
则按输出错误处理,不能得分。

样例输入:
6
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98

样例输出:
6 265
4 264
3 258
2 244
1 237

import java.util.*;

public class Main {
    private static class Grade {
        Integer num;
        Integer chinese;
        Integer grade;

        public Grade(int num, int chinese, int grade) {
            this.num = num;
            this.chinese = chinese;
            this.grade = grade;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = Integer.parseInt(scanner.nextLine());
        Set<Grade> set = new TreeSet<>(new Comparator<Grade>() {//set是自动排序
            public int compare(Grade grade1, Grade grade2) {
                int result1 = -grade1.grade.compareTo(grade2.grade);
                if (result1 != 0) {//先比成绩
                    return result1;
                }
                int result2 = -grade1.chinese.compareTo(grade2.chinese);
                if (result2 != 0) {//后比语文成绩 
                    return result2;
                }
                return grade1.num.compareTo(grade2.num);//最后比学号
            }
        });
        for (int i = 1; i <= num; i++) {
            String[] array = scanner.nextLine().split(" ");
            int chinese = Integer.parseInt(array[0]);
            int grade = chinese + Integer.parseInt(array[1]) + Integer.parseInt(array[2]);
            set.add(new Grade(i, chinese, grade));
        }
        int counter = 0;
        for (Grade grade : set) {
            counter++;
            System.out.println(grade.num + " " + grade.grade);
            if (counter == 5) {
                break;
            }
        }
        scanner.close();
    }
}

写Comparable那个东西太抽象了

第四题 外卖店优先级

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值