第十四届蓝桥杯大赛软件赛省赛C/C++ 大学 B 组
- A题:日期统计
- B题:01串的熵
- C题:冶炼金属
- D题:飞机降落
- E题:接龙数列
A题:日期统计
直接遍历2023年每一天,看数组中是否有符合的
java的coding如下:
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如下:
package mainimport "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 := 0for 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 := 0for i := 0; i < 100; i++ {if array[i] == dateSeq[k] {k++if k == 8 {ans++break}}}}}fmt.Println(ans)
}
B题:01串的熵
枚举即可
注意设置变量为浮点型,避免多余的类型转换
java的coding如下:
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如下:
package mainimport ("fmt""math"
)func main() {n := 23333333.0var sum float64for o := 0; o <= int(n/2); o++ {sum = 0sum -= 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如下:
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如下:
package mainimport ("fmt"
)func main() {var n intfmt.Scan(&n)a := make([]int, n)b := make([]int, n)for i := 0; i < n; i++ {var A, B intfmt.Scan(&A, &B)a[i] = A / Bb[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如下:
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如下:
package mainimport "fmt"type plane struct {t, d, l int
}var vis [15]bool
var flag bool
var p [15]planefunc dfs(m, cnt, last int) {if cnt == m {flag = truereturn}for i := 0; i < m; i++ {if !vis[i] && p[i].t+p[i].d >= last {vis[i] = truedfs(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 intfmt.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 = falsedfs(N, 0, 0)if flag {fmt.Println("YES")} else {fmt.Println("NO")}}
}
E题:接龙数列
咳咳,这边暴力只通过40%
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如下:
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如下:
package mainimport ("fmt"
)func main() {var n, x, y, m intfmt.Scan(&n)dp := make([]int, 10)for i := 0; i < n; i++ {var s stringfmt.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}