第十四届蓝桥杯大赛软件赛省赛C/C++ 大学 B 组A-E题(go、java实现)

第十四届蓝桥杯大赛软件赛省赛C/C++ 大学 B 组

A题:日期统计

直接遍历2023年每一天,看数组中是否有符合的

java的coding如下:

java 复制代码
public class Main {
    public static void main(String[] args) {
        int[] array = {
            5, 6, 8, 6, 9, 1, 6, 1, 2, 4, 9, 1, 9, 8, 2, 3, 6, 4, 7, 7,
            5, 9, 5, 0, 3, 8, 7, 5, 8, 1, 5, 8, 6, 1, 8, 3, 0, 3, 7, 9,
            2, 7, 0, 5, 8, 8, 5, 7, 0, 9, 9, 1, 9, 4, 4, 6, 8, 6, 3, 3,
            8, 5, 1, 6, 3, 4, 6, 7, 0, 7, 8, 2, 7, 6, 8, 9, 5, 6, 5, 6,
            1, 4, 0, 1, 0, 0, 9, 4, 8, 0, 9, 1, 2, 8, 5, 0, 2, 5, 3, 3
        };

        int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int ans = 0;

        for (int month = 1; month <= 12; ++month) {
            for (int day = 1; day <= daysInMonth[month]; ++day) {
                int[] dateSeq = {2, 0, 2, 3, month / 10, month % 10, day / 10, day % 10};
                int k = 0;

                for (int i = 0; i < 100; ++i) {
                    if (array[i] == dateSeq[k]) {
                        ++k;
                        if (k == 8) {
                            ans++;
                            break;
                        }
                    }
                }
            }
        }

        System.out.println(ans);
    }
}

go的coding如下:

go 复制代码
package main

import "fmt"

func main() {
    array := []int{
        5, 6, 8, 6, 9, 1, 6, 1, 2, 4, 9, 1, 9, 8, 2, 3, 6, 4, 7, 7,
        5, 9, 5, 0, 3, 8, 7, 5, 8, 1, 5, 8, 6, 1, 8, 3, 0, 3, 7, 9,
        2, 7, 0, 5, 8, 8, 5, 7, 0, 9, 9, 1, 9, 4, 4, 6, 8, 6, 3, 3,
        8, 5, 1, 6, 3, 4, 6, 7, 0, 7, 8, 2, 7, 6, 8, 9, 5, 6, 5, 6,
        1, 4, 0, 1, 0, 0, 9, 4, 8, 0, 9, 1, 2, 8, 5, 0, 2, 5, 3, 3,
    }

    daysInMonth := []int{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    ans := 0

    for month := 1; month <= 12; month++ {
        for day := 1; day <= daysInMonth[month]; day++ {
            dateSeq := [8]int{2, 0, 2, 3, month / 10, month % 10, day / 10, day % 10}
            k := 0

            for i := 0; i < 100; i++ {
                if array[i] == dateSeq[k] {
                    k++
                    if k == 8 {
                        ans++
                        break
                    }
                }
            }
        }
    }

    fmt.Println(ans)
}

B题:01串的熵

枚举即可

注意设置变量为浮点型,避免多余的类型转换

java的coding如下:

java 复制代码
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        double n = 23333333;
        for (int i = 0; i <= n / 2; i++) {
          double sum = 0;
          sum -= i * (i / n) * Math.log(i / n) / Math.log(2) + (n - i) * ((n - i) / n) * Math.log((n - i) / n) / Math.log(2);
          if (sum > 11625907.5 && sum < 11625907.6) {
            System.out.println(i);
            break;
          } 
        }

        scan.close(); 
    }
}

go的coding如下:

go 复制代码
package main

import (
    "fmt"
    "math"
)

func main() {
    n := 23333333.0
    var sum float64

    for o := 0; o <= int(n/2); o++ {
        sum = 0
        sum -= float64(o) * (float64(o) / n) * math.Log(float64(o)/n)/math.Log(2) + (n - float64(o)) * ((n - float64(o)) / n) * math.Log((n - float64(o))/n)/math.Log(2)
        if sum > 11625907.5 && sum < 11625907.6 {
            fmt.Println(o)
            break
        }
    }
}

C题:冶炼金属

推公式题目

java的coding如下:

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] a = new int[n];
        int[] b = new int[n];
        for (int i = 0; i < n; i++) {
            int A = scan.nextInt();
            int B = scan.nextInt();
            a[i] = A / B;
            b[i] = A / (B + 1);
        }
        int maxB = b[0];
        int minA = a[0];
        for (int i = 1; i < n; i++) {
            maxB = Math.max(maxB, b[i]);
            minA = Math.min(minA, a[i]);
        }
        System.out.println((maxB + 1) + " " + minA);
        scan.close();
    }
}

go的coding如下:

go 复制代码
package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	a := make([]int, n)
	b := make([]int, n)

	for i := 0; i < n; i++ {
		var A, B int
		fmt.Scan(&A, &B)
		a[i] = A / B
		b[i] = A / (B + 1)
	}

	maxB := b[0]
	minA := a[0]

	for i := 1; i < n; i++ {
		if b[i] > maxB {
			maxB = b[i]
		}
		if a[i] < minA {
			minA = a[i]
		}
	}

	fmt.Println(maxB+1, minA)
}

D题:飞机降落

DFS暴力搜索即可 记得回溯

java的coding如下:

java 复制代码
import java.util.Scanner;

public class Main {
    static class Plane {
        int t, d, l;
    }

    static boolean[] vis = new boolean[15];
    static boolean flag;
    static Plane[] p = new Plane[15];

    public static void dfs(int m, int cnt, int last) {
        if (cnt == m) {
            flag = true;
            return;
        }
        for (int i = 0; i < m; i++) {
            if (!vis[i] && p[i].t + p[i].d >= last) {
                vis[i] = true;
                dfs(m, cnt + 1, Math.max(last, p[i].t) + p[i].l);
                vis[i] = false;
            }
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int T = scan.nextInt();
        for (int t = 0; t < T; t++) {
            int N = scan.nextInt();
            for (int i = 0; i < N; i++) {
                p[i] = new Plane();
                p[i].t = scan.nextInt();
                p[i].d = scan.nextInt();
                p[i].l = scan.nextInt();
            }
            flag = false;
            dfs(N, 0, 0);
            if (flag) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
        scan.close();
    }
}

go的coding如下:

go 复制代码
package main

import "fmt"

type plane struct {
    t, d, l int
}

var vis [15]bool
var flag bool
var p [15]plane

func dfs(m, cnt, last int) {
    if cnt == m {
        flag = true
        return
    }
    for i := 0; i < m; i++ {
        if !vis[i] && p[i].t+p[i].d >= last {
            vis[i] = true
            dfs(m, cnt+1, max(last, p[i].t)+p[i].l)
            vis[i] = false
        }
    }
}

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}

func main() {
    var T, N int
    fmt.Scan(&T)
    for t := 0; t < T; t++ {
        fmt.Scan(&N)
        for i := 0; i < N; i++ {
            fmt.Scan(&p[i].t, &p[i].d, &p[i].l)
        }
        flag = false
        dfs(N, 0, 0)
        if flag {
            fmt.Println("YES")
        } else {
            fmt.Println("NO")
        }
    }
}

E题:接龙数列

咳咳,这边暴力只通过40%

java 复制代码
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(scan.next());
        }
        scan.close();
        int res = 0;
        
        Iterator<String> it = list.iterator();
        String prev = it.next();
        while (it.hasNext()) {
            String current = it.next();
            if (prev.charAt(prev.length() - 1) != current.charAt(0)) {
                it.remove();
                res++;
                n--;
            } else {
                prev = current;
            }
        }
        
        System.out.println(res);
    }
}

所以需要使用动态规划

根据数列总长度-最长接龙数列长度等于最少删除次数求最优解, 定义状态为当前最长接龙数列长度,则dp[i]就是以i为数字最后一位的最长接龙数列长度,设x为当前数字的第一位(如果为接龙数列,也就是前一位数的最后一位),y为当前数字的最后一位,则转移方程可以写为dp[y]=max(dp[x]+1,dp[y])

java的coding如下:

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] dp = new int[10];
        int m = 0;
        for (int i = 0; i < n; i++) {
            String s = scan.next();
            int x = s.charAt(0) - '0';
            int y = s.charAt(s.length() - 1) - '0';
            dp[y] = Math.max(dp[x] + 1, dp[y]); //选与不选 不选即是删除
            m = Math.max(m, dp[y]);
        }
        System.out.println(n - m);
        scan.close();
    }
}

go的coding如下:

go 复制代码
package main

import (
	"fmt"
)

func main() {
	var n, x, y, m int
	fmt.Scan(&n)
	dp := make([]int, 10)
	for i := 0; i < n; i++ {
		var s string
		fmt.Scan(&s)
		x = int(s[0] - '0')
		y = int(s[len(s)-1] - '0')
		dp[y] = max(dp[x]+1, dp[y])
		m = max(m, dp[y])
	}
	fmt.Println(n - m)
}

func max(a, b int) int {if a > b { return a }; return b}
相关推荐
冰帝海岸10 分钟前
01-spring security认证笔记
java·笔记·spring
世间万物皆对象36 分钟前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了1 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
小二·1 小时前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic2 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
懒洋洋大魔王2 小时前
RocketMQ的使⽤
java·rocketmq·java-rocketmq
武子康2 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神2 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
qq_327342733 小时前
Java实现离线身份证号码OCR识别
java·开发语言
阿龟在奔跑4 小时前
引用类型的局部变量线程安全问题分析——以多线程对方法局部变量List类型对象实例的add、remove操作为例
java·jvm·安全·list