本文共 3125 字,大约阅读时间需要 10 分钟。
第十一届蓝桥杯大赛软件类省赛C/C++大学B组试题分析
作为一名技术人员,我有幸参与了第十一届蓝桥杯大赛软件类省赛C/C++大学B组的比赛。这场比赛不仅锻炼了我的编程能力,还让我深刻体会到了编写高效代码的重要性。以下是我对观察到的几道题目的分析和解决方案。
给定一个初始值 tot = 10000
以及一个布尔变量 go
,我们需要通过循环操作来最终计算出一个答案。
go
为 true
时,我们将 tot
减少 600,直到 tot
达到 600 以下。在这一轮循环中,我们还需要将 go
设为 false
,以便进入下一步。go
为 false
时,我们将 tot
增加 300,并将 go
设为 true
,这样就进入了下一轮循环。ans
递增 1。最终,我们需要对最终的 ans
进行处理,将其乘以 60 并加上 tot
除以 10 的结果作为最终输出。
public class Main { public static void main(String[] args) { int tot = 10000; boolean go = true; int ans = 0; while (true && tot >= 0) { if (go) { if (tot - 600 < 0) break; tot -= 600; go = false; } else { tot += 300; go = true; } ans++; } System.out.println(ans * 60 + tot / 10); }}
给定两个日期格式为 “yyyy-MM-dd HH:mm:ss”
,我们需要计算两个日期之间的时间差,并以秒为单位输出结果。
SimpleDateFormat
解析两个日期字符串,分别转换为 Date
对象。getTime()
方法将日期转换为毫秒数。import java.text.SimpleDateFormat;import java.util.Date;public class Main { public static void main(String[] args) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date begin = sdf.parse("1921-7-23 12:00:00"); Date end = sdf.parse("2020-7-1 12:00:00"); long ans = end.getTime() - begin.getTime(); ans /= 60000; System.out.println(ans); }}
给定一个 n x m
的二维数组 dp
,dp[1][1]
的值为 1。我们需要根据一定规则填充其他单元格的值。
dp[1][1]
为 1。dp[i][j]
,如果其当前位置是奇数行或奇数列,则将它的值加上从上方或左方转移的值。dp[n][m]
的值。import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int[][] dp = new int[n + 5][m + 5]; dp[1][1] = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if ((i & 1) == 1 || (j & 1) == 1) { dp[i][j] += dp[i][j - 1] + dp[i - 1][j]; } } } System.out.println(dp[n][m]); }}
给定一个字符串输入,我们需要将其转换为对应的一个多位数。
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); char[] a = s.toCharArray(); StringBuilder ans = new StringBuilder(); for (int i = 1; i < a.length; i++) { if (a[i] >= '2' && a[i] <= '9') { for (int j = 0; j < a[i] - '0'; j++) { ans.append(a[i - 1]); } } else if (a[i - 1] < '2' || a[i - 1] > '9') { ans.append(a[i - 1]); } } if (a.length == 1) { ans.append(a[0]); } System.out.println(ans.toString()); }}
这些题目涵盖了循环、日期计算、数组填数以及字符串处理等多个技术内容。通过深入分析每一道题目的解法,我不仅提升了自己的编程能力,也更好地理解了如何在复杂环境中高效解决问题。希望未来能在更多的竞赛中继续挑战自己!
转载地址:http://kqhzk.baihongyu.com/