Java && C++ 实现第十二届蓝桥杯 C++ B组 省赛真题(希望能和各位佬能一起讨论算法题:讨论群:99979568)

Posted 发呆哥o_o ....

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java && C++ 实现第十二届蓝桥杯 C++ B组 省赛真题(希望能和各位佬能一起讨论算法题:讨论群:99979568)相关的知识,希望对你有一定的参考价值。

山水有相逢 来日皆可期
告辞

虽然是C++组的,但是和Java组的差不了两道题,大家都可以看一看
如有错误,还请佬 评论或私信指出(写的稍些急)
等后面在补充补充解题思路,在补一套C++实现(已完成,简单题未补C++版本代码)
再也不肝博客了
/(ㄒoㄒ)/~~

(由于上传图片有限制,如果描述不清楚点击手写题解)
手写题解字丑,但是小编感觉会比文字看的直观一些(其实是不会用数学公式)

编程题测试数据

手写题解(如果访问出错,请刷新访问的页面即可(Nebo的问题))

A:空间
B:卡片
C:直线
D:货物摆放
E:路径
F:时间显示
G:砝码称重
H:杨辉三角形
I:双向排序
J:括号序列

当前页面的编程题均在C语言网成功运行
C语言网有各届蓝桥杯的题库
第十二届蓝桥杯编程题测试
刷题集

A:空间

package LanqiaobeiExam._12CB;

/**
 * ClassName: A空间
 * Package: LanqiaobeiExam._12CB
 *
 * @DATE: 2022/3/21 15:01
 * Author: asleep
 */
public class A空间   //1B = 8bit(位)    32位 = 4B  1MB = 1024KB = 1024 * 1024B
    public static void main(String[] args)     //67108864
        System.out.println(256 * 1024 * 1024 / 4);
    


B:卡片

package LanqiaobeiExam._12CB;

/**
 * ClassName: B卡片
 * Package: LanqiaobeiExam._12CB
 *
 * @DATE: 2022/3/21 15:19
 * Author: asleep
 */
public class B卡片   //3181      从1开始循环数字,每个数字循环每一位,对应数字卡片 -1,减到 0 表示当前数字不能拼成
    public static void main(String[] args) 
        int[] count = new int[10];
        for (int i = 0; i < 10; i++) 
            count[i] = 2021;
        
        int index = 0;
        A:
        while (true) 
            int temp = ++index;
            while (temp != 0) 
                if (count[temp % 10] > 0) 
                    count[temp % 10]--;
                 else 
                    System.out.println(index - 1);
                    break A;
                
                temp /= 10;
            
        

    


C:直线


package LanqiaobeiExam._12CB;

import java.util.*;

/**
 * ClassName: C直线
 * Package: LanqiaobeiExam._12CB
 *
 * @DATE: 2022/3/21 15:29
 * Author: asleep
 */
public class C直线 	//40257
    public static class Node 
        int x, y;

        public Node(int x, int y) 
            this.x = x;
            this.y = y;
        

        double x1, y1;

        public Node(double x1, double y1) 
            this.x1 = x1;
            this.y1 = y1;
        


    

    public static class Line 
        int a, b, c;

        public Line(int a, int b, int c) 
            this.a = a;
            this.b = b;
            this.c = c;
        

        @Override
        public boolean equals(Object o) 
            Line line = (Line) o;
            return a == line.a && b == line.b && c == line.c;
        

        @Override
        public int hashCode() 
            return Objects.hash(a, b, c);
        
    

    public static int gcd(int a, int b) 
        return b == 0 ? a : gcd(b, a % b);
    

    public static int gcd(int a, int b, int c) 
        return gcd(gcd(a, b), gcd(b, c));
    

    public static void main(String[] args) 
        //方法一   两点式
        

            int n = 20, m = 21;
            Set<Line> set = new HashSet<>();
            Node[] node = new Node[1000];
            int index = 0;
            for (int i = 0; i < n; i++) 
                for (int j = 0; j < m; j++) 
                    node[index++] = new Node(i, j);
                
            
            for (int i = 0; i < index; i++) 
                for (int j = i + 1; j < index; j++) 
                    int a = node[i].y - node[j].y;//系数
                    int b = node[j].x - node[i].x;
                    int c = node[i].y * (node[i].x - node[j].x) - node[i].x * (node[i].y - node[j].y);
                    int g = gcd(Math.abs(a), Math.abs(b), Math.abs(c));
                    set.add(new Line(a / g, b / g, c / g));
                
            
            System.out.println(set.size());

        

        //方法二:  y = kx+b 确定直线
        

            List<Integer> points = new ArrayList<>();
            Set<String> ans = new HashSet<>();
            for (int i = 0; i < 20; i++) 
                for (int j = 0; j < 21; j++) 
                    points.add(i * 100 + j); //存xy
                
            
            int size = points.size();
            for (int i = 0; i < size; i++) 
                int node1 = points.get(i);
                int x1 = node1 / 100, y1 = node1 % 100;
                for (int j = i + 1; j < size; j++) 
                    int node2 = points.get(j);
                    int x2 = node2 / 100, y2 = node2 % 100;
                    int up = y1 - y2, down = x1 - x2;
                    if (down == 0) 
                        ans.add("x = " + x1);
                        continue;
                    
                    int c1 = gcd(up, down);
                    String K = up / c1 + " " + down / c1;
                    int Up = y1 * down - x1 * up;
                    int c2 = gcd(Up, down);
                    String B = Up / c2 + " " + down / c2;
                    ans.add(K + " " + B);
                
            
            System.out.println(ans.size());

        

        //方法三:
        
            //两点式两个点交换后为一条新的直线,需要除 2
            Set<Line> set = new HashSet<>();
            int n = 20, m = 21;
            for (int x1 = 0; x1 < n; x1++) 
                for (int y1 = 0; y1 < m; y1++) 
                    for (int x2 = 0; x2 < n; x2++) 
                        for (int y2 = 0; y2 < m; y2++) 
                            if (x1 == x2 && y1 == y2)  //两个相同的点不能确定直线
                                continue;
                            
                            int a =y1 - y2, b = x2 - x1;
                            int c = x1 * (y2 - y1) - y1 * (x2 - x1);
                            int g = gcd(Math.abs(a), Math.abs(b), Math.abs(c));
                            set.add(new Line(a / g, b / g, c / g));
                        
                    
                
            
            System.out.println(set.size() / 2);

        

    


#include "iostream"
#include "algorithm"
#include "cmath"

using namespace std;

struct Node    //40257
    double k, b;
    bool operator< (const Node& node) const   //自定义排序方法,先按照k的大小排序,k相等时按照b的大小排序
        if (k != node.k) 
            return k < node.k;
        
        return b < node.b;
    
nodes[200000];;

int main() 
    int index= 0;   //遍历两个xy
    for (int x1 = 0; x1 < 20; x1++) 
        for (int y1 = 0; y1 < 21; y1++) 
            for (int x2 = 0; x2 < 20; x2++) 
                for (int y2 = 0; y2 < 21; y2++) 
                    if (x1 == x2)  //斜率是0,一共有二十条,后面直接加
                        continue;
                    
                    double k = (double)(y2 - y1) / (x2 - x1);
                    double b = y1 - k * x1;
                    nodes[index++] = k, b;
                
            
        
    
    sort(nodes, nodes + index);
    int res = 1;    //默认第一个就存在了
    for (int i = 1; i < index; i++)    //小数是有误差的,当误差比10的-8次方小,我们就认为是一条直线
        if (fabs(nodes[i].k - nodes[i - 1].k) > 1e-8 || fabs(nodes[i].b - nodes[i - 1].b) > 1e-8) 
            res++;
        
    
    cout << res + 20;

    return 0;


D:货物摆放

package LanqiaobeiExam._12CB;

/**
 * ClassName: D货物摆放
 * Package: LanqiaobeiExam._12CB
 *
 * @DATE: 2022/3/21 17:00
 * Author: asleep
 */
public class D货物摆放     //2430
    public static void main(String[] args)     //直接遍历 n 会炸,我们这里遍历 n 的约数,相对复杂度会低很多
        long n = 2021041820210418L;
        long[] res = new long[10000];
        int index = 0;
        for (long i = 1L; i <= n / i; i++) 
            if (n % i == 0) 
                res[index++] = i;
                if (i * i != n) 
                    res[index++] = n / i;
                
            
        
        int count = 0;
        for (int i = 0; i < index; i++) 
            for (int j = 0; j < index; j++) 
                if (n % (res[i] * res[j]) == 0) 
                    count++;
                
            
        
        System.out.println(count);
    


E:路径

Dijkstra最短路径

package LanqiaobeiExam._12CB;

import java.util.Arrays;

/**
 * ClassName: E路径
 * Package: LanqiaobeiExam._12CB
 *
 * @DATE: 2022/3/21 17:08
 * Author: asleep
 */
public class E路径   //10266837
    static int[][] map = new int[2022][2022];
    public static int gcd(int a, int b) 
        return b == 0 ? a : gcd(b, a % b);
    
    public static int dijkstra() 
        int[] distance = new int[2022];
        Arrays.fill(distance, 0xffffff);
        boolean[] visited = new boolean[2022];
        distance[1] = 0;
        for (int i = 1; i < 2022; i++) 
            int temp = -1;
            for (int j = 1; j 以上是关于Java && C++ 实现第十二届蓝桥杯 C++ B组 省赛真题(希望能和各位佬能一起讨论算法题:讨论群:99979568)的主要内容,如果未能解决你的问题,请参考以下文章

课程总结

第十一周 &十二周&十三周周记

第十二篇 Python函数之全局变量&局部变量&递归函数

Golang✔️走进 Go 语言✔️ 第十二课 结构体 & 切片

Golang✔️走进 Go 语言✔️ 第十二课 结构体 & 切片

Java实现第十二届蓝桥杯 C++ B组 省赛真题(希望能和各位佬能一起讨论算法题:讨论群:99979568)