当前位置: 首页 > news >正文

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可以指出来,我只是个菜鸟,请多多指教

相关文章:

  • 软考——WWW与HTTP
  • 【R语言】ggplot2绘图常用操作
  • 安卓cmake修改版本设置路径
  • 校园的网络安全
  • GPT-4 它不仅仅是 ChatGPT 的升级版,更是人工智能的一次革命性突破!简单原理剖析
  • JSON Web Token在登陆中的使用
  • 在大数据项目中如何确保数据的质量和准确性的
  • 七.智慧城市数据治理平台架构
  • 微信小程序页面导航与路由:实现多页面跳转与数据传递
  • DeepSeek-R1:通过强化学习激发大语言模型的推理能力
  • JVM生产环境问题定位与解决实战(三):揭秘Java飞行记录器(JFR)的强大功能
  • C#开发——如何捕获异常和抛出异常
  • PHP入门基础学习五(函数1)
  • 黑客入门(网络安全术语解释)
  • DeepSeek为云厂商带来新机遇,东吴证券看好AI带动百度智能云增长
  • JVM可用的垃圾回收器
  • C++ openssl AES/CBC/PKCS7Padding 256位加密 解密示例 MD5示例
  • 某项目自动化测试分享
  • 抗干扰利器,光纤无人机技术详解
  • CNN 卷积神经网络
  • 手机端网站建设的注意事项/百度风云榜游戏
  • 为什么有的公司做很多个网站/企业网站设计毕业论文
  • 网站建设合同服务事项/优化seo可以从以下几个方面进行
  • 惠民建设局网站是哪个/快链友情链接平台
  • 石家庄做网站推广/电商网站大全
  • asp文件怎么做网站/公司企业网站建设方案