2021年 第12届 蓝桥杯 Java B组 省赛真题详解及小结【第1场省赛 2021.04.18】

总分:70分

一、试题A:ASC  得分:5分

本题总分:5 分

【问题描述】

        已知大写字母 A 的 ASCII 码为 65,请问大写字母 L 的 ASCII 码是多少?

【答案提交】

        这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

【答案】:76

import javax.print.DocFlavor;
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main
{
    static Scanner sc = new Scanner(System.in);
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)3000+ 10;

    public static void main(String[] args) throws IOException
    {
        char c = 'L';
        pw.println((int)c);
        pw.flush();
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

class PII
{
    int x,y;
    public PII(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

class Edge
{
    int a,b,c;
    public Edge(int a ,int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

二、试题B:卡片 

本题总分:5 分

【问题描述】

        小蓝有很多数字卡片,每张卡片上都是数字 0 到 9。

        小蓝准备用这些卡片来拼一些数,他想从 1 开始拼出正整数,每拼一个,就保存起来,卡片就不能用来拼其它数了。

        小蓝想知道自己能从 1 拼到多少。

        例如,当小蓝有 30 张卡片,其中 0 到 9 各 3 张,则小蓝可以拼出 1 到 10,

        但是拼 11 时卡片 1 已经只有一张了,不够拼出 11。

        现在小蓝手里有 0 到 9 的卡片各 2021 张,共 20210 张,请问小蓝可以从 1 拼到多少?

        提示:建议使用计算机编程解决问题。

【答案提交】

        这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

【答案】:3181

不会

三、试题C:直线

本题总分:10 分

【问题描述】

        在平面直角坐标系中,两点可以确定一条直线。如果有多点在一条直线上,那么这些点中任意两点确定的直线是同一条。

        给定平面上 2 × 3 个整点 {(x, y)|0 ≤ x < 2, 0 ≤ y < 3, x ∈ Z, y ∈ Z},即横坐标是 0 到 1 (包含 0 和 1) 之间的整数、纵坐标是 0 到 2 (包含 0 和 2) 之间的整数的点。这些点一共确定了 11 条不同的直线。

        给定平面上 20 × 21 个整点 {(x, y)|0 ≤ x < 20, 0 ≤ y < 21, x ∈ Z, y ∈ Z},即横坐标是 0 到 19 (包含 0 和 19) 之间的整数、纵坐标是 0 到 20 (包含 0 和 20) 之间的整数的点。请问这些点一共确定了多少条不同的直线。

【答案提交】

        这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

【答案】:40257

不会

思路:

涉及到直线问题我们需要考虑斜率和截距:

直线: y = k x + b, k 是斜率, b是截距,我们可以用一个类来存斜率和截距。

第一步:在给定范围内,任意两个点之间构成一条直线

第二步:求出非重边的条数

需要注意的点:

① 因为斜率可能是小数,所以要用浮点型数据进行比较,对于 d o u b l e   a , b,如果 ∣a−b∣≤1e−8 则 a , b 相等。

② 非重边:斜率或者截距不相等

③ 因为垂直于x轴的直线有20条,斜率和截距都相等,因此最后统计非重边条数的时候要+20

AC代码:(补题)

#这个方法速度快,但是精度要求高的话可能会错

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.*;

public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)200010;

    static Line[] line = new Line[N];
    static int n;
    static Mycomparator cmp = new Mycomparator();

    public static void main(String[] args ) throws IOException, ParseException
    {
        for (int x1 = 0; x1 <= 19; x1++)
        {
            for (int y1 = 0; y1 <= 20; y1++)
            {
                for (int x2 = 0; x2 <= 19; x2++)
                {
                    for (int y2 = 0; y2 <= 20; y2++)
                    {
                        if (x1 != x2)
                        {
                            double k = (double) (y2 - y1) / (x2 - x1);
                            double b = y1 - k * x1;
                            line[n++] = new Line(k, b);
                        }
                    }
                }
            }
        }

        Arrays.sort(line, 0, n,cmp);

        int res = 1;
        for (int i = 1; i < n; i++)
        {
            // 求斜率或者截距不等
            if (Math.abs(line[i].k - line[i - 1].k) > 1e-8 || Math.abs(line[i].b - line[i - 1].b) > 1e-8) {
                res++;
            }
        }

        pw.println(res + 20); // 最后要加上20
        pw.flush();
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException  { return reader.readLine(); }

    static String next() throws IOException
    {
        while (!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException  { return Integer.parseInt(next()); }

    static double nextDouble() throws IOException { return Double.parseDouble(next()); }

    static long nextLong() throws IOException  { return Long.parseLong(next());}

    static BigInteger nextBigInteger() throws IOException { return new BigInteger(rd.nextLine()); }
}

class PII
{
    int x,y;

    public PII(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

class Line
{
    double k;
    double b;

    public Line(double k, double b)
    {
        this.k = k;
        this.b = b;
    }
}

class Mycomparator implements Comparator<Line>
{
    @Override
    public int compare(Line o1, Line o2)
    {
        if(o1.k < o2.k)  return 1;
        if(o1.k == o2.k)
        {
            if(o1.b < o2.b)  return 1;
            return -1;
        }
        return -1;
    }
}

#这个方法速度慢,但是精度要求高的话肯定对

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.*;

public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)200010;

    static Line[] line = new Line[N];
    static int n;
    static Mycomparator cmp = new Mycomparator();

    public static void main(String[] args ) throws IOException, ParseException
    {
        for (int x1 = 0; x1 <= 19; x1++)
        {
            for (int y1 = 0; y1 <= 20; y1++)
            {
                for (int x2 = 0; x2 <= 19; x2++)
                {
                    for (int y2 = 0; y2 <= 20; y2++)
                    {
                        if (x1 != x2)
                        {
                            double k = (double) (y2 - y1) / (x2 - x1);
                            double b = y1 - k * x1;
                            line[n++] = new Line(k, b);
                        }
                    }
                }
            }
        }

        Arrays.sort(line, 0, n,cmp);

        int res = 1;
        for (int i = 1; i < n; i++)
        {
            // 求斜率或者截距不等
            if (Math.abs(line[i].k - line[i - 1].k) > 1e-8 || Math.abs(line[i].b - line[i - 1].b) > 1e-8) {
                res++;
            }
        }

        pw.println(res + 20); // 最后要加上20
        pw.flush();
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException  { return reader.readLine(); }

    static String next() throws IOException
    {
        while (!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException  { return Integer.parseInt(next()); }

    static double nextDouble() throws IOException { return Double.parseDouble(next()); }

    static long nextLong() throws IOException  { return Long.parseLong(next());}

    static BigInteger nextBigInteger() throws IOException { return new BigInteger(rd.nextLine()); }
}

class PII
{
    int x,y;

    public PII(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

class Line
{
    double k;
    double b;

    public Line(double k, double b)
    {
        this.k = k;
        this.b = b;
    }
}

class Mycomparator implements Comparator<Line>
{
    @Override
    public int compare(Line o1, Line o2)
    {
        BigDecimal k1 = new BigDecimal(String.valueOf(o1.k));
        BigDecimal k2 = new BigDecimal(String.valueOf(o2.k));
        BigDecimal b1 = new BigDecimal(String.valueOf(o1.b));
        BigDecimal b2 = new BigDecimal(String.valueOf(o2.b));

        if(k1.compareTo(k2) < 0)  return 1;
        if(k1.compareTo(k2) > 0)  return -1;
        if(k1.compareTo(k2) == 0)
        {
            if(b1.compareTo(b2) < 0)  return 1;
            return -1;
        }
        return -1;
    }
}

四、试题D:货物摆放  得分:10分

本题总分:10 分

【问题描述】

        小蓝有一个超大的仓库,可以摆放很多货物。

        现在,小蓝有 n 箱货物要摆放在仓库,每箱货物都是规则的正方体。小蓝规定了长、宽、高三个互相垂直的方向,每箱货物的边都必须严格平行于长、 宽、高。

        小蓝希望所有的货物最终摆成一个大的立方体。即在长、宽、高的方向上分别堆 L、W、H 的货物,满足 n = L × W × H。

        给定 n,请问有多少种堆放货物的方案满足要求。

        例如,当 n = 4 时,有以下 6 种方案:1×1×4、1×2×2、1×4×1、2×1×2、2 × 2 × 1、4 × 1 × 1。

        请问,当 n = 2021041820210418 (注意有 16 位数字)时,总共有多少种方案?

        提示:建议使用计算机编程解决问题。

【答案提交】

        这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

【答案】:2430

方法一:

思路:遍历这个大数的所有因数,然后对这些因数进行全排序,找到所有三个相乘为大数的排序,要注意的一点是得对大数取个平方根,加快速度。

枚举2021041820210418的约数即可,对约数进行多重循环枚举,对枚举出来的三个数字进行全排列。即可得出答案。

AC代码:

import javax.management.openmbean.TabularDataSupport;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
 
public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static math math_myself = new math();
    static int N = (int)2e5 + 10;
    static Deque<Long> temp = new LinkedList<>();
    static List<Long> yn = new ArrayList<>();
    static long cnt = 0;
    static long n = 2021041820210418L;
 
    // 三个因子对应三个位置
    static void dfs(long now) // now表示当前的数为多少
    {
        if(temp.size() == 3) // 暂存因子的数组有三个元素(满足题目要求)
        {
            if(now == n)   cnt++; // 当前的数为n且n由三个因子相乘而来
            return;
        }
 
        for(int i = 0 ; i < yn.size() ; i ++)
        {
            temp.addLast(yn.get(i));
            dfs(now * yn.get(i));
            temp.removeLast();
        }
    }
 
    public static void main(String[] args) throws IOException
    {
        yn = math_myself.get_factor(n);
        dfs(1);
        pw.println(cnt);
        pw.flush();
    }
}
 
class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");
 
    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}
 
class PII
{
    int x,y;
    public PII(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}
 
class math
{
    long gcd(long a,long b)
    {
        if(b == 0)  return a;
        else return gcd(b,a % b);
    }
 
    long lcm(long a,long b)
    {
        return a * b / gcd(a, b);
    }
 
    // 求n的所有约数
    List get_factor(long n)
    {
        List<Long> a = new ArrayList<>();
        for(long i = 1; i <= Math.sqrt(n) ; i ++)
        {
            if(n % i == 0)
            {
                a.add(i);
                if(i != n / i)  a.add(n / i);  // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况  ^-^复杂度能减少多少是多少
            }
        }
 
        // 相同因子去重,这个方法,完美
        a = a.stream().distinct().collect(Collectors.toList());
 
        // 对因子排序(升序)
        Collections.sort(a);
 
        return a;
    }
}

方法二:

思路:

给出一个数n,求多少个三元组(L,W,H)使得L x W x H等于n。同时三元组是考虑顺序的,L,W,H是n的因数,即n % L == 0 && n % W == 0 && n % H == 0,为此,我们可以先将n的所有因数求出来,然后三重循环遍历L,W,H,若它们相乘等于n,则找到了一种方案。(暴力)

AC代码:

import javax.management.openmbean.TabularDataSupport;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
 
public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static math math_myself = new math();
    static int N = (int)2e5 + 10;
    static Deque<Long> temp = new LinkedList<>();
    static List<Long> list = new ArrayList<>();
    static long cnt = 0;
    static long n = 2021041820210418L; // 数的后边的加上“L”,将此数字转换成long类型
 
    public static void main(String[] args) throws IOException
    {
        list = math_myself.get_factor(n);
 
        for(int i = 0 ; i < list.size() ; i ++)
        {
            for(int j = 0 ; j < list.size() ; j ++)
            {
                if(list.get(i) * list.get(j) > n)  continue;
                for(int k = 0 ; k < list.size() ; k ++)
                {
                    if(list.get(i) * list.get(j) * list.get(k) == n)  cnt ++;
                }
            }
        }
        pw.println(cnt);
        pw.flush();
    }
}
 
class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");
 
    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}
 
class PII
{
    int x,y;
    public PII(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}
 
class math
{
    long gcd(long a,long b)
    {
        if(b == 0)  return a;
        else return gcd(b,a % b);
    }
 
    long lcm(long a,long b)
    {
        return a * b / gcd(a, b);
    }
 
    // 求n的所有约数
    List get_factor(long n)
    {
        List<Long> a = new ArrayList<>();
        for(long i = 1; i <= Math.sqrt(n) ; i ++)
        {
            if(n % i == 0)
            {
                a.add(i);
                if(i != n / i)  a.add(n / i);  // 去重 // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况  ^-^复杂度能减少多少是多少
            }
        }
 
        // 对因子排序(升序)
        Collections.sort(a);
 
        return a;
    }
}

五、试题E:路径  得分:15分

本题总分:15 分

【问题描述】

        小蓝学习了最短路径之后特别高兴,他定义了一个特别的图,希望找到图中的最短路径。

        小蓝的图由 2021 个结点组成,依次编号 1 至 2021。

        对于两个不同的结点 a, b,如果 a 和 b 的差的绝对值大于 21,则两个结点之间没有边相连;如果 a 和 b 的差的绝对值小于等于 21,则两个点之间有一条 长度为 a 和 b 的最小公倍数的无向边相连。

        例如:结点 1 和结点 23 之间没有边相连;结点 3 和结点 24 之间有一条无向边,长度为 24;结点 15 和结点 25 之间有一条无向边,长度为 75。

        请计算,结点 1 和结点 2021 之间的最短路径长度是多少。

        提示:建议使用计算机编程解决问题。

【答案提交】

        这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

【答案】:10266837

矩阵建图(最大公约数GCD,最小公倍数)然后 floyd 或 dijkstra,暴力即可求出! 

floyd: 

import javax.management.openmbean.TabularDataSupport;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import java.math.*;
 
public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static math math_myself = new math();
    static int INF = (int)1e9;
    static int N = (int)2022;
    static int a[][] = new int[N][N];
 
    public static void main(String[] args) throws IOException
    {
        int res;
        for(int i = 1 ; i <= 2021 ; i ++)
        {
            for(int j = 1 ; j <= 2021 ; j ++)
            {
                a[i][j] = Math.abs(i - j) > 21? INF: (int) math_myself.lcm(i, j);
            }
        }
 
 
        // 模板
        for(int k = 1 ; k <= 2021 ; k ++)
        {
            for(int i = 1 ; i <= 2021 ; i ++)
            {
                for (int j = 1 ; j <= 2021 ; j ++)
                {
                    a[i][j] = Math.min(a[i][j], a[i][k] + a[k][j]);
                }
            }
        }
 
        pw.println(a[1][2021]);
        pw.flush();
    }
}
 
class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");
 
    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}
 
class PII
{
    int x,y;
    public PII(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}
 
class math
{
    long gcd(long a,long b)
    {
        if(b == 0)  return a;
        else return gcd(b,a % b);
    }
 
    long lcm(long a,long b)
    {
        return a * b / gcd(a, b);
    }
 
    // 求n的所有约数
    List get_factor(long n)
    {
        List<Long> a = new ArrayList<>();
        for(long i = 1; i <= Math.sqrt(n) ; i ++)
        {
            if(n % i == 0)
            {
                a.add(i);
                if(i != n / i)  a.add(n / i);  // 去重 // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况  ^-^复杂度能减少多少是多少
            }
        }
 
        // 对因子排序(升序)
        Collections.sort(a);
 
        return a;
    }
}

dijkstra: 

import javax.management.openmbean.TabularDataSupport;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import java.math.*;
 
public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static math math_myself = new math();
    static int INF = (int)1e9;
    static int N = (int)2022;
    static int g[][] = new int[N][N];
    static int dist[] = new int[N]; // 存每个点到起点的距离
    static boolean visited[] = new boolean[N]; // true表示i号点已经确定了最短距离,false表示没有确定
 
    static int Dijkstra(int n)
    {
        Arrays.fill(dist,Integer.MAX_VALUE);
 
        dist[1] = 0; // 1号点距离起点(1号点)的距离已经确定了
        // n次循环,每次循环确定距离起点最短的点(有n个点所以要进行n次迭代)
        for(int i = 1 ; i <= n ; i ++)
        {
            int t = -1; // 将t设置为-1,因为Dijkstra算法适用于不存在负权的图,t存的是当前访问的点
            // 每次迭代的过程中,我们都先找到当前未确定的距离起点最短的点
            for(int j = 1 ; j <= n ; j ++) // 这里的循环代表的是从1号点开始检索
            {
                if(!visited[j] && (t == -1 || dist[j] < dist[t]))  t = j;
            }
 
            // 通过上述操作当前我们的t代表就是剩下未确定最短路的点中距离起点最短的点,与此同时,该点的最短路径也已经确定,我们将此点标记
            visited[t] = true;
 
            // 依次刚确定的点到它的相邻点的距离
            for(int j = 1 ; j <= n ; j ++)  dist[j] = Math.min(dist[j],dist[t] + g[t][j]);
        }
        if(dist[n] == Integer.MAX_VALUE)  return -1;
        return dist[n];
    }
 
    public static void main(String[] args) throws IOException
    {
        int n = 2021;
        // 建立图
        for(int i = 1 ; i <= n ; i ++)
        {
            for(int j = 1 ; j <= n ; j ++)
            {
                if(i == j)  g[i][j] = 0;
                else  g[i][j] = Math.abs(i - j) > 21? INF: (int) math_myself.lcm(i, j);
            }
        }
 
        pw.println(Dijkstra(2021));
 
        pw.flush();
    }
}
 
class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");
 
    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}
 
class PII
{
    int x,y;
    public PII(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}
 
class math
{
    long gcd(long a,long b)
    {
        if(b == 0)  return a;
        else return gcd(b,a % b);
    }
 
    long lcm(long a,long b)
    {
        return a * b / gcd(a, b);
    }
 
    // 求n的所有约数
    List get_factor(long n)
    {
        List<Long> a = new ArrayList<>();
        for(long i = 1; i <= Math.sqrt(n) ; i ++)
        {
            if(n % i == 0)
            {
                a.add(i);
                if(i != n / i)  a.add(n / i);  // 去重 // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况  ^-^复杂度能减少多少是多少
            }
        }
 
        // 对因子排序(升序)
        Collections.sort(a);
 
        return a;
    }
}

六、试题F:时间显示   得分:15分

时间限制: 1.0s 内存限制: 512.0MB 本题总分:15 分

【问题描述】

        小蓝要和朋友合作开发一个时间显示的网站。在服务器上,朋友已经获取了当前的时间,用一个整数表示,值为从 1970 年 1 月 1 日 00:00:00 到当前时刻经过的毫秒数。

        现在,小蓝要在客户端显示出这个时间。小蓝不用显示出年月日,只需要显示出时分秒即可,毫秒也不用显示,直接舍去即可。

        给定一个用整数表示的时间,请将这个时间对应的时分秒输出。

【输入格式】

        输入一行包含一个整数,表示时间。

【输出格式】

        输出时分秒表示的当前时间,格式形如 HH:MM:SS,其中 HH 表示时,值为 0 到 23,MM 表示分,值为 0 到 59,SS 表示秒,值为 0 到 59。时、分、秒不足两位时补前导 0。

【样例输入 1】46800999

【样例输出 1】13:00:00

【样例输入 2】1618708103123

【样例输出 2】01:08:23

【评测用例规模与约定】对于所有评测用例,给定的时间为不超过 1018 的正整数。

思路及其函数接口
h–/60---->min------/60----->s-----/1000----->ms
1、%d就是普通的输出了

2、%2d是将数字按宽度为2,采用右对齐方式输出,若数据位数不到2位,则左边补空格:

3、%02d,和% 2d差不多,只不过左边补0

修饰符 格式说明 意义
1、M

%md 以宽度m输出整型数,不足m时,左补空格

2、0m

%0md 以宽度m输出整型数,不足m时,左补零

3、m,n

%m.nf宽度m输出实型小数小数位为n位

AC代码:
 

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;

public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = 200010;

    static Line[] line = new Line[N];
    static int n;

    public static void main(String[] args) throws IOException
    {
        //在此输入您的代码...
        long n = rd.nextLong();
        n /= 1000;
        // 1s = 1000ms
        // 1min = 60s = 60000ms
        // 1h = 60min = 3600s = 3600000ms
        pw.printf("%02d:%02d:%02d", (n % (3600 * 24)) / 3600,(n % 3600) / 60,n % 60);
        pw.flush();
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

class math
{
    int gcd(int a,int b)
    {
        if(b == 0)  return a;
        else return gcd(b,a % b);
    }

    int lcm(int a,int b)
    {
        return a * b / gcd(a, b);
    }

    // 求n的所有约数
    List get_factor(int n)
    {
        List<Long> a = new ArrayList<>();
        for(long i = 1; i <= Math.sqrt(n) ; i ++)
        {
            if(n % i == 0)
            {
                a.add(i);
                if(i != n / i)  a.add(n / i);  // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况  ^-^复杂度能减少多少是多少
            }
        }

        // 相同因子去重,这个方法,完美
        a = a.stream().distinct().collect(Collectors.toList());

        // 对因子排序(升序)
        Collections.sort(a);

        return a;
    }

    // 判断是否是质数
    boolean check_isPrime(int n)
    {
        if(n < 2) return false;
        for(int i = 2 ; i <= n / i; i ++)  if (n % i == 0) return false;
        return true;
    }
}

class PII implements Comparable<PII>
{
    int x,y;
    public PII(int x ,int y)
    {
        this.x = x;
        this.y = y;
    }
    public int compareTo(PII a)
    {
        if(this.x-a.x != 0)
            return this.x-a.x;  //按x升序排序
        else return this.y-a.y;  //如果x相同,按y升序排序
    }
}

class Edge
{
    int a,b,c;
    public Edge(int a ,int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

class Line implements Comparable<Line>
{
    double k; // 斜率
    double b; // 截距

    public Line(double k, double b)
    {
        this.k = k;
        this.b = b;
    }

    @Override
    public int compareTo(Line o)
    {
        if (this.k > o.k) return 1;
        if (this.k == o.k)
        {
            if (this.b > o.b) return 1;
            return -1;
        }
        return -1;
    }
}

七、试题G:最少砝码

时间限制: 1.0s 内存限制: 512.0MB 本题总分:20 分

【问题描述】

        你有一架天平。现在你要设计一套砝码,使得利用这些砝码可以称出任意小于等于 N 的正整数重量。

        那么这套砝码最少需要包含多少个砝码?

        注意砝码可以放在天平两边。

【输入格式】输入包含一个正整数 N。

【输出格式】输出一个整数代表答案。

【样例输入】7

【样例输出】3

【样例说明】

        3 个砝码重量是 1、4、6,可以称出 1 至 7 的所有重量。

        1 = 1;

        2 = 6 − 4 (天平一边放 6,另一边放 4);

        3 = 4 − 1;

        4 = 4;

        5 = 6 − 1;

        6 = 6;

        7 = 1 + 6;

        少于 3 个砝码不可能称出 1 至 7 的所有重量。

【评测用例规模与约定】对于所有评测用例,1 ≤ N ≤ 1000000000。

 不会

八、试题H:杨辉三角形   得分:6分

时间限制: 5.0s 内存限制: 512.0MB 本题总分:20 分

【问题描述】

        下面的图形是著名的杨辉三角形:

        如果我们按从上到下、从左到右的顺序把所有数排成一列,可以得到如下数列:

                                                        

         1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, ...

        给定一个正整数 N,请你输出数列中第一次出现 N 是在第几个数?

【输入格式】输入一个整数 N。

【输出格式】输出一个整数代表答案。

【样例输入】6

【样例输出】13

【评测用例规模与约定】对于 20% 的评测用例,1 ≤ N ≤ 10;对于所有评测用例,1 ≤ N ≤ 1000000000。

过掉30%的样例的代码:

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)1e3 + 10;
    static int a[][] = new int[N][N];

    public static void main(String[] args ) throws IOException
    {
        int n = rd.nextInt();
        a[1][1] = 1;
        for(int i = 2 ; i < N ; i ++)
        {
            for(int j = 1 ; j <= i ; j ++)
            {
                a[i][j] = (a[i - 1][j] + a[i - 1][j - 1]);
            }
        }


        int cnt = 1;
        for(int i = 2 ; i < N ; i ++)
        {
            for(int j = 1 ; j <= i ; j ++)
            {
                cnt ++;
                if(a[i][j] == n)
                {
                    pw.println(cnt);
                    pw.flush();
                    return;
                }
            }
        }

    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

class PII implements Comparable<PII>
{
    int x,y;
    public PII(int x ,int y)
    {
        this.x = x;
        this.y = y;
    }
    public int compareTo(PII a)
    {
        if(this.x-a.x != 0)
            return this.x-a.x;  //按x升序排序
        else return this.y-a.y;  //如果x相同,按y升序排序
    }
}

class Edge
{
    int a,b,c;
    public Edge(int a ,int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

 

九、试题I:双向排序   得分:15分

时间限制: 5.0s 内存限制: 512.0MB 本题总分:25 分

【问题描述】

        给定序列 (a1, a2, · · · , an) = (1, 2, · · · , n),即 ai = i。

        小蓝将对这个序列进行 m 次操作,每次可能是将 a1, a2, · · · , aqi 降序排列,或者将 aqi , aqi+1, · · · , an 升序排列。

        请求出操作完成后的序列。

【输入格式】

        输入的第一行包含两个整数 n, m,分别表示序列的长度和操作次数。

        接下来 m 行描述对序列的操作,其中第 i 行包含两个整数 pi , qi 表示操作类型和参数。当 pi = 0 时,表示将 a1, a2, · · · , aqi 降序排列;当 pi = 1 时,表示将 aqi , aqi+1, · · · , an 升序排列。

【输出格式】输出一行,包含 n 个整数,相邻的整数之间使用一个空格分隔,表示操作完成后的序列。

【样例输入】

        3 3

        0 3

        1 2

        0 2

【样例输出】 3 1 2

【样例说明】

        原数列为 (1, 2, 3)。

        第 1 步后为 (3, 2, 1)。

        第 2 步后为 (3, 1, 2)。

        第 3 步后为 (3, 1, 2)。与第 2 步操作后相同,因为前两个数已经是降序了。

【评测用例规模与约定】

        对于 30% 的评测用例,n, m ≤ 1000;

        对于 60% 的评测用例,n, m ≤ 5000;

        对于所有评测用例,1 ≤ n, m ≤ 100000,0 ≤ ai ≤ 1,1 ≤ bi ≤ n。

过掉60%的的代码:(sort模拟)

import java.io.*;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)1e5 + 10;
    static Integer a[] = new Integer[N];
    static MyComparator cmp = new MyComparator();

    public static void main(String[] args) throws NumberFormatException, IOException
    {
        int n = rd.nextInt();
        int m = rd.nextInt();

        for(int i = 1 ; i <= n ; i ++)  a[i] = i;

        while(m -- > 0)
        {
            int p = rd.nextInt();
            int q = rd.nextInt();
            if(p == 0)
            {
                Arrays.sort(a,1,q + 1,cmp);
            }
            if(p == 1)
            {
                Arrays.sort(a,q,n + 1);
            }
        }

        for(int i = 1 ; i <= n ; i ++)  pw.print(a[i] + " ");
        pw.flush();
    }
}

class MyComparator implements Comparator<Integer>
{
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2 - o1;
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

class PII implements Comparable<PII>
{
    int x,y;
    public PII(int x ,int y)
    {
        this.x = x;
        this.y = y;
    }
    public int compareTo(PII a)
    {
        if(this.x-a.x != 0)
            return this.x-a.x;  //按x升序排序
        else return this.y-a.y;  //如果x相同,按y升序排序
    }
}

class Edge
{
    int a,b,c;
    public Edge(int a ,int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

十、试题J:括号序列

时间限制: 5.0s 内存限制: 512.0MB 本题总分:25 分

【问题描述】

        给定一个括号序列,要求尽可能少地添加若干括号使得括号序列变得合法,当添加完成后,会产生不同的添加结果,请问有多少种本质不同的添加结果。两个结果是本质不同的是指存在某个位置一个结果是左括号,而另一个是右括号。

        例如,对于括号序列 (((),只需要添加两个括号就能让其合法,有以下几 种不同的添加结果:()()()、()(())、(())()、(()()) 和 ((()))。

【输入格式】输入一行包含一个字符串 s,表示给定的括号序列,序列中只有左括号和 右括号。

【输出格式】输出一个整数表示答案,答案可能很大,请输出答案除以 1000000007 (即10^9 + 7) 的余数。

【样例输入】 ((()

【样例输出】 5

【评测用例规模与约定】

        对于 40% 的评测用例,|s| ≤ 200。

        对于所有评测用例,1 ≤ |s| ≤ 5000。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

21RGHLY

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

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

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

打赏作者

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

抵扣说明:

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

余额充值