2024团体程序设计天梯赛-总决赛 JAVA L1部分

L2正在制作中~(其实就是我菜没做完哈哈哈比赛时L2才60分,等我整理整理~)

字符串题的快读模板在结尾...

点击题目标题可跳转至题目集---

目录

L1-1 编程解决一切

L1-2 再进去几个人

L1-3 帮助色盲

L1-4 四项全能

 L1-5 别再来这么多猫娘了!

L1-6 兰州牛肉面

L1-7 整数的持续性

L1-8 九宫格


L1-1 编程解决一切

public class Main {
    public static void main(String[] args) {
            System.out.println("Problem? The Solution: Programming.");
    }
}

L1-2 再进去几个人

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner star = new Scanner(System.in);
        System.out.println(Math.abs(star.nextInt()-star.nextInt()));
    }
}

L1-3 帮助色盲

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner star = new Scanner(System.in);
        int a = star.nextInt();
        int b = star.nextInt();
        if (a==2)
            System.out.println("-\nstop");
        else if (b==1)
            System.out.println(a==1?"-\nmove":"-\nstop");
        else if (a==1)
            System.out.println("dudu\nmove");
        else if (a==0)
            System.out.println("biii\nstop");
    }
}

L1-4 四项全能

(注意所有人都全能的情况)

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner star = new Scanner(System.in);
        int s = star.nextInt();
        int num = star.nextInt();
        int cnt = 0;//技能点
        for (int i = 0; i < num; i++) {
            int a = star.nextInt();
            cnt+=a;
        }
        if (cnt/s<num-1)        //技能点都不够每个人num-1个技能
            System.out.println(0);
        else if (cnt/s==num-1)  //样例的情况
            System.out.println(cnt%s);
        else                    //所有人啥都会
            System.out.println(s);
    }
}

 L1-5 别再来这么多猫娘了!

想必大家也被这题恶心了,我只想说,出题人我<censored>你<censored>

思路:

这题有一个很重要的地方,就是违禁词有可能是<censored>的子串,那么就会进入循环导致超时,卡三个测试点,所以我们先把他替换成一个特殊的字符,因为他题目的字符包括大小写字母、数字、空格及 ASCII 码范围内的标点符号的文字,不熟悉的同学可以和我一样用汉字代替,在最后计数完毕统一替换,Java实现时,需要注意replaceAll和replace方法,我们要用replace,因为replaceAll是通过正则表达式替换的,比如在替换\\\\时他前两\是转义字符,而replace是直接替换字符串,卡的是测试点7,不懂的同学可以看看这个博主的文章:关于replace和replaceAll

以下是AC代码:(熟悉的快读快输,虽然这题不卡这个时间,不过养个习惯)

import java.io.*;
public class Main {
    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    public static void main(String[] args) throws IOException {
        int n = Integer.parseInt(bf.readLine());
        String[] wj = new String[n];
        for (int i = 0; i < wj.length; i++) {
            wj[i] = bf.readLine();
        }
        int max = Integer.parseInt(bf.readLine());
        String str = bf.readLine();
        for (int i = 0; i < n; i++) {
            if (str.contains(wj[i])) {
                str = str.replace(wj[i], "星");
            }
        }
        int cnt = 0;//违禁词数量
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i)=='星')
                cnt++;
        }
        if (cnt>=max){
            out.println(cnt+"\nHe Xie Ni Quan Jia!");
        }else {
            str = str.replaceAll("星", "<censored>");
            out.println(str);
        }
        out.flush();
    }
}

L1-6 兰州牛肉面

emmm...变量名起的有点抽象,比赛时没想那么多,不好意思啦大家~

import java.io.*;
public class Main {
    static StreamTokenizer star = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static void main(String[] args) throws IOException {
        int n = nextInt();
        double[] ps = new double[n+1];
        int[] cnt = new int[n+1];
        for (int i = 1; i < ps.length; i++) {
            ps[i] = nextDou();
        }
        double all = 0;
        while (true){
            int zl = nextInt();
            int ws = nextInt();
            if (zl==0)
                break;
            else {
                cnt[zl]+=ws;//对应种类的碗数
                all+=ps[zl]*ws;//算账
            }
        }
        for (int i = 1; i <= n; i++) {
            System.out.println(cnt[i]);
        }
        System.out.printf("%.2f",all);
    }
    static int nextInt() throws IOException{
        star.nextToken();
        return (int)star.nval;
    }static double nextDou() throws IOException{
        star.nextToken();
        return star.nval;
    }
}

L1-7 整数的持续性

思路:直接枚举每个数,如果持续性和当前max一样就装到集合里,如果更大就把集合清空再装进去&&更新max

import java.util.*;

public class Main {
    static int cnt = 0;
    public static void main(String[] args) {
        Scanner star = new Scanner(System.in);
        int a = star.nextInt();
        int b = star.nextInt();
        ArrayList<Integer> list = new ArrayList<>();
        int max = Integer.MIN_VALUE;
        for (int i = a; i <= b; i++) {
            int cxx = cxx(i);
            if (cxx==max){
                list.add(i);
            }else if (cxx>max){
                list.clear();
                list.add(i);
                max = cxx;
            }
            cnt = 0;
        }
        System.out.println(max);
        for (int i = 0; i < list.size(); i++) {
            System.out.print(i== list.size()-1?list.get(i):list.get(i)+" ");
        }
    }
    static int cxx(int n){
        while (n>=10){
            n = cj(n);
            cnt++;
        }
        return cnt;
    }
    static int cj(int num){
        int n = 1;
        while (num!=0){
            n*=(num%10);
            num/=10;
        }
        return n;
    }
}

L1-8 九宫格

直接就是暴力!循环(循环)(循环(循环(...))))

import java.io.*;
import java.util.*;
public class Main {
    static StreamTokenizer star = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static void main(String[] args) throws IOException{
        int n = nextInt();
        while (n-->0){
            int[][] x = new int[9][9];
            for (int i = 0; i < x.length; i++) {
                for (int j = 0; j < x[i].length; j++) {
                    x[i][j] = nextInt();
                }
            }
            if (hang(x)&&lie(x)&&jiu(x))
                System.out.println(1);
            else
                System.out.println(0);
        }
    }
    static int nextInt() throws IOException {
        star.nextToken();
        return (int)star.nval;
    }
    static boolean hang(int[][] x){
        Set<Integer> set = new HashSet<>();
        for (int[] ints : x) {
            for (int anInt : ints) {
                if (anInt > 9||anInt < 1)
                    return false;
                set.add(anInt);
            }
            if (set.size() != 9)
                return false;
            set.clear();
        }
        return true;
    }
    static boolean lie(int[][] x){
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < x.length; i++) {
            for (int j = 0; j < x[i].length; j++) {
                if (x[j][i]>9||x[j][i]<1)
                    return false;
                set.add(x[j][i]);
            }
            if (set.size()!=9)
                return false;
            set.clear();
        }
        return true;
    }
    static boolean jiu(int[][] x){
        for (int i = 0; i < 9; i+=3) {
            for (int j = 0; j < 9; j+=3) {
                Set<Integer> set = new HashSet<>();
                for (int a = i; a < i+3; a++) {
                    for (int b = j; b < j+3; b++) {
                        if (x[a][b]>9||x[a][b]<1)
                            return false;
                        set.add(x[a][b]);
                    }
                }
                if (set.size()!=9)
                    return false;
                set.clear();
            }
        }
        return true;
    }
}

字符串题快读模板 

输入数字要按照这个格式,因为readLine会换行,Scanner不会换行,混用会出问题的...

import java.io.*;
public class Main {
    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) throws IOException {
        int n = nextInt();
        String str = next();
    }
    static int nextInt() throws IOException{
        return Integer.parseInt(bf.readLine());
    }static String next() throws IOException{
        return bf.readLine();
    }
}

感谢支持,可以点赞关注以后一起学习~ 

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

劳逸结合的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值