2020第五届上海第二工业大学新生程序设计竞赛(Java题解)

2 篇文章 1 订阅

2020第五届上海第二工业大学新生程序设计竞赛(Java题解)
作为C/C++版本的补充题解,仅供参考
需要解析的可以看这篇:
2020第五届上海第二工业大学新生程序设计竞赛

用Java语言参加竞赛时,对数据量很大的题目,需要额外对I/O进行优化,不然会超时。(有些出题人没考虑其他语言时,可能会导致Java无解,要有心理准备,不过新生赛没事)
想学IO优化的同学可以另行百度=.=

A-迎新小游戏——抽奖品

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long a = sc.nextInt();
        long b = sc.nextInt();

        if (a == 1) {
            System.out.println('0');
            return;
        }

        long fenzi = a * (a - 1);
        long fenmu = (a + b) * (a + b - 1);
        long gck = gcd(fenzi, fenmu);
        fenzi /= gck;
        fenmu /= gck;
        System.out.println(fenzi + "/" + fenmu);
        
    }

    public static long gcd(long a, long b) {
        long min = Math.min(a, b);
        long max = Math.max(a, b);
        if (max % min == 0) return min;
        max %= min;
        return gcd(max, min);
    }

}

B-Coda’s Fibonacci Password

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[11];
        arr[1] = arr[2] = 1;
        for (int i = 2; i < 11; i++) {
            arr[i] = arr[i-1] + arr[i-2];
        }
        System.out.println(arr[sc.nextInt()]);
    }

}

C-密室

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        char[] chars = str.toCharArray();
        int x= 0,y = 0;
        for (int i = 0; i < chars.length; i++) {
            if(chars[i] == 'l' && x >0){
                x--;
            }else if(chars[i] == 'r' && x < 99){
                x++;
            }else if(chars[i] == 'u' && y > 0){
                y--;
            }else if(chars[i] == 'd' && y < 99){
                y++;
            }
        }
        System.out.println(x + " "  + y);

    }
}

D-迎新小游戏——送礼物

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        if( b % a == 0){
            System.out.println("0");
            return;
        }
        int f = b / a+1;
        System.out.println(f*a-b);

    }
}

E-CSGO与IU

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        char[] chars = s.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            if(i +1 < chars.length && (chars[i] == 'i' && chars[i+1] == 'u')){
                chars[i] = 'I';
                chars[i+1] = 'U';
                i++;
            }

            if(i + 3 < chars.length && (chars[i] == 'c' && chars[i+1] == 's'&& chars[i+2] == 'g'&& chars[i+3] == 'o')){
                chars[i] = 's';
                chars[i+1] = 's';
                chars[i+2] = 'p';
                chars[i+3] = 'u';
                i+=3;
            }
        }
        System.out.println(String.valueOf(chars));

    }
}

F-迎新小游戏——排队形

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int n,m,p,q;
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        p = sc.nextInt();
        q = sc.nextInt();
        long[][][][] f = new long[110][110][20][20];
        int mod = 998244353;
        for(int i=1;i<=p;i++) f[i][0][i][0]=1;
        for(int i=1;i<=q;i++) f[0][i][i][1]=1;
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=m;j++) {
                for(int k=1;k<=Math.min(j,q);k++){
                    f[i][j][1][0]+=f[i-1][j][k][1];
                    f[i][j][1][0]%=mod;
                }
                for(int k=1;k<=Math.min(i,p);k++) {
                    f[i][j][1][1]+=f[i][j-1][k][0];
                    f[i][j][1][1] %= mod;
                }
                for(int k=2;k<=Math.min(i,p);k++) {
                    f[i][j][k][0]+=f[i-1][j][k-1][0];
                    f[i][j][k][0] %= mod;
                }
                for(int k=2;k<=Math.min(j,q);k++) {
                    f[i][j][k][1]+=f[i][j-1][k-1][1];
                    f[i][j][k][1] %= mod;
                }
            }
        }
        long ans=0;
        for(int i=1;i<=p;i++) {
            ans+=f[n][m][i][0];
            ans %=mod;
        }
        for(int i=1;i<=q;i++) {
            ans+=f[n][m][i][1];
            ans %= mod;
        }
       System.out.println(ans);

    }
}

G-A Broken Ancient Robot

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        sc.next();
        System.out.println("Error!");
    }
}

H-Raki的学习小剧场

import java.io.*;
import java.util.Arrays;

public class Main {
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

    static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    public static void main(String[] args) throws IOException {
        int n = nextInt();
        int k = nextInt();
        Thing[] arr = new Thing[n];
        int[] ints = new int[k];

        for (int i = 0; i < n; i++) {
            arr[i] = new Thing(nextInt(), nextInt(), nextInt(), i + 1);
        }

        Arrays.sort(arr);
        for (int i = 0; i < k; i++) {
            ints[i] = arr[i].id;
        }
        Arrays.sort(ints);

        out.print(ints[0]);
        for (int i = 1; i < k; i++) {
            out.print(" " + ints[i]);
        }
        out.flush();
    }
}

class Thing implements Comparable {
    int v;
    int id;

    Thing(int a, int b, int c, int id) {
        v = a / b * c;
        this.id = id;
    }

    @Override
    public int compareTo(Object o) {
        Thing t = (Thing) o;
        if (v > t.v) return -1;
        if (v == t.v) if (id < t.id) return -1;
        return 1;
    }
}

I-新冠病毒之解救Coda

import java.io.*;

public class Main {
    static long[] sum, an;
    static int n;
    static long k;

    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    static long nextLong() throws IOException {
        in.nextToken();
        return (long) in.nval;
    }

    public static void main(String[] args) throws IOException {
        n = (int)nextLong();
        k = nextLong();
        sum = new long[2000000+1];
        an = new long[2000000+1];

        for (int i = 1; i <= n; i++) {
            an[i] = nextLong();
            sum[i] = sum[i - 1] + an[i];
        }

        int l = 0, r = n;
        while (l < r) {
            int mid = (l + r + 1) / 2;
            if (check(mid)) l = mid;
            else r = mid - 1;
        }
        System.out.println(l);
    }

    public static boolean check(int len) {
        for (int l = 1; l + len - 1 <= n; l++) {
            int r = l + len - 1;
            if (sum[r] - sum[l - 1] <= k) return true;
        }
        return false;
    }

}

J-旅游难题

import java.io.*;

public class Main {
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

    static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    public static void main(String[] args) throws IOException {
        int T = nextInt();
        for (int i = 0; i < T; i++) {
            int n = nextInt();
            int m = nextInt();
            if (m >= n - 1) out.print("YES");
            else out.print("NO");
            if (m >= n) out.println(" YES");
            else out.println(" NO");
        }
        out.flush();

    }
}

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值