2021年蓝桥杯javaB组第二场题目+部分解析

一、 求余    1

 请 问 2021 % 20 的 值 是 多 少?

public static void main(String[] args) {

    System.out.println(2021%20);

}

二、双阶乘    59375

 解一

public static void main(String[] args) {

   long n=1;

     for(int i=2021;i>0;i-=2) {

       n*=i;

        if(n>100000) {

        n%=100000;

         }

      }

   System.out.println(n);



}


解二

    public static void main(String[] args) {

        long ans = 1;

        for (int i = 3; i <= 2021; i++) {

            if (i % 2 == 1) {

                ans = ans * i % 100000;

            }

        }

        System.out.println(ans);

    }


三 、格点   15698

暴力解 嘿嘿

public static void main(String[] args) {
 
   long sum=0;

   for(int i=1;i<=2021;i++) {

   for(int j=1;j<=2021;j++) {

    if(i*j<=2021) {

      sum++;

      }

     }

   }
  System.out.println(sum);



}

四、整数分解(剪枝优化、记忆化搜索dfs)、DP691677274345

Dfs
   

 static long[][] f = new long[6][2022];

public static void main(String[] args) {

     // 5个数凑出2021

for (int i = 0; i < 6; i++) {

Arrays.fill(f[i], -1);

}

System.out.println(dfs(5, 2021));

    }

    static long dfs(int n, int sum) {

     if (f[n][sum] != -1) return f[n][sum];

     if (n == 0) {

     if (sum == 0) {

     return 1;

     }

     return 0;

     }

     f[n][sum] = 0;

     for (int i = 1; i <= sum; i++) {

     f[n][sum] += dfs(n - 1, sum - i);

     }

     return f[n][sum];

    }

剪枝优化 

public static void main(String[] args) {

        long ans = 0;

        for (int i = 1; i <= 2021; i++) {

            for (int j = 1; j <= 2021; j++) {

                if (i + j >= 2021) {

                    break;

                }

                for (int k = 1; k <= 2021; k++) {

                  

                    int tmp = 2021 - i - j - k;

                   

                    if (tmp >= 2) {

                        ans += tmp - 1;

                    } else {

                        break;

                    }

                }

            }

        }

        System.out.println(ans);

    }

DP

    public static void main(String[] args) {

    int n = 2021, k = 5;

        long[][] dp = new long[k + 1][n + 1];

        Arrays.fill(dp[1], 1);

        for (int i = 2; i <= k; i++)

            for (int j = i; j <= n; j++)

                dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1];

        System.out.println(dp[k][n]);

    }

五、城邦(最小生成树)4046

有空在写代码,嘿嘿


六、特殊年份

暴力解:

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);



int count=0;

for(int i=0;i<5;i++) {

int n=sc.nextInt();

if(n/1000%10==n/10%10&&n%10>n/100%10) {

count++;

}



}System.out.println(count);

}

----------------------------------------------------------------

七 、 小平方

public static void main(String[] args) {

   Scanner sc=new Scanner(System.in);

     int n=sc.nextInt();

     long count=0;

     for(int i=1;i<n;i++) {

       if((Math.pow(i, 2)%n)<(n/2)){

          count++;

     }

   }

  System.out.println(count);

}


八完全平方数(数学定理)

 

public static void main(String[] args) {

   Scanner sc=new Scanner(System.in);

     Double n=(double) sc.nextInt();

     int a=0;

     for(int i=1;i<1000;i++) {

       double number=Math.sqrt(i*n);

       if(number%1==0) {

        a=i;

        break;

}

}

System.out.println(a);

}


九、负载均衡(模拟 + 优先队列(堆))

 

public static void main(String[] args) { new Dome9().run(); }



    void run() {

        InputReader in = new InputReader(System.in);

        PrintWriter out = new PrintWriter(System.out);

        int n = in.readInt(), m = in.readInt();

        Queue<Item>[] cpt = new Queue[n + 1];

        int[] cptd = new int[n + 1];

        for (int i = 1; i <= n; i++) {

            cpt[i] = new PriorityQueue();

            cptd[i] = in.readInt();

        }

        while (m-- > 0) {

            int a = in.readInt();

            int b = in.readInt();

            int c = in.readInt();

            int d = in.readInt();

            while (cpt[b].size() > 0 && cpt[b].peek().c <= a)

                cptd[b] += cpt[b].poll().d;

            if (cptd[b] >= d) {

                cpt[b].offer(new Item(a + c, d));

                out.println(cptd[b] -= d);

            } else out.println("-1");

        }

        out.flush();

    }



    class Item implements Comparable<Item> {



        int c, d;



        Item(int c, int d) {

            this.c = c;

            this.d = d;

        }



        public int compareTo(Item item) { return this.c - item.c; }

    }



    class InputReader {



        BufferedReader reader;

        StringTokenizer token;



        InputReader(InputStream in) {

            this.reader = new BufferedReader(new InputStreamReader(in));

        }



        String read() {

            while (token == null || !token.hasMoreTokens()) {

                try {

                    token = new StringTokenizer(reader.readLine());

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

            return token.nextToken();

        }



        int readInt() { return Integer.parseInt(read()); }

    }

十、国际象棋(dfs

以上有什么bug可以指出来,我只是个菜鸟,请多多指教

### 2024 蓝桥杯 Java B 真题合集 为了帮助参赛者更好地准备蓝桥杯竞赛,以下是整理的自第四届至第十四届蓝桥杯 Java B 省赛真题及其解析。 #### 第四届到第十届蓝桥杯 Java B 真题概述 从2013的第四届开始,蓝桥杯都会举办省级选拔赛。这些赛事涵盖了多种类型的编程挑战,包括但不限于结果填空、代码填空以及程序设计等不同形式的问题[^1]。 - **第四至第六届**:早期的比赛主要集中在基础算法的理解与应用上,难度逐渐提升。 - **第七至第九届**:此阶段增加了更多复杂的数据结构操作和高级算法的应用场景。 - **第十届**:进一步提升了对综合能力的要求,不仅考察选手的基础知识掌握情况,更注重实际解决问题的能力。 #### 十一届以后的变化与发展 进入2021后,蓝桥杯继续保持着高水准命题风格的同时,在内容设置方面更加贴近行业发展趋势和技术热点[^2]。 - **第十一届**:引入了一些新兴技术领域内的案例分析作为背景材料,使得题目既具有学术价值又不失趣味性和实用性。 - **第十二届**:强化了对于大数据处理能力和多线程并发控制技巧等方面的考核力度。 - **第十三届**:特别强调了安全编码意识的重要性,并通过具体实例引导学生思考如何构建更为健壮可靠的软件系统。 #### 关于最新的一届——第十四届蓝桥杯 针对刚刚结束不久的2024第十四届蓝桥杯Java B比赛,官方提供了详细的试题解答指南。其中提到一些关键知识点如大整数运算(BigInteger)、动态规划等问题求解方法得到了广泛应用。值得注意的是,在面对某些特定类型的大规模数据计算时,合理利用数学规律可以有效简化问题并提高效率[^3]。 ```java import java.math.BigInteger; public class FactorialCalculation { public static void main(String[] args) { BigInteger result = factorial(new BigInteger("100")); System.out.println(result); } private static BigInteger factorial(BigInteger n){ if(n.equals(BigInteger.ZERO)){ return BigInteger.ONE; }else{ return n.multiply(factorial(n.subtract(BigInteger.ONE))); } } } ``` 上述代码展示了使用 `BigInteger` 类来解决超出了基本数值范围的情况下的阶乘计算问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值