当前位置:   article > 正文

C语言算法赛——蓝桥杯(省赛试题)_蓝桥杯c语言题目省赛

蓝桥杯c语言题目省赛

目录

一、十四届C/C++程序设计C组试题

二、十四届C/C++程序设计B组试题

三、十四届C/C++程序设计A组试题

四、十三届C/C++程序设计C组试题

五、十三届C/C++程序设计B组试题

六、十三届C/C++程序设计A组试题

七、十二届C/C++程序设计C组试题

八、十二届C/C++程序设计B组试题

九、十二届C/C++程序设计A组试题

十、十一届C/C++程序设计C组试题


一、十四届C/C++程序设计C组试题

  1. 十四届程序C组试题A
  2. #include <stdio.h>
  3. int main()
  4. {
  5. long long sum = 0;
  6. int n = 20230408;
  7. int i = 0;
  8. // 累加从1到n的所有整数
  9. for (i = 1; i <= n; i++)
  10. {
  11. sum += i;
  12. }
  13. // 输出结果
  14. printf("%lld\n", sum);
  15. return 0;
  16. }

  1. //十四届程序C组试题B
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include<time.h>
  6. // 时间字符串解析为结构体 tm
  7. void parseTime(char* timeString, struct tm* timeStruct) {
  8. sscanf(timeString, "%d-%d-%d %d:%d:%d",
  9. &timeStruct->tm_year, &timeStruct->tm_mon, &timeStruct->tm_mday,
  10. &timeStruct->tm_hour, &timeStruct->tm_min, &timeStruct->tm_sec);
  11. // tm_year表示的是自1900年以来的年数,需要减去1900
  12. timeStruct->tm_year -= 1900;
  13. // tm_mon表示的是0-11的月份,需要减去1
  14. timeStruct->tm_mon -= 1;
  15. }
  16. // 每一对相邻的上下班打卡之间的时间差
  17. int calculateTimeDifference(char* time1, char* time2) {
  18. struct tm start, end;
  19. // 解析时间字符串为结构体 tm
  20. parseTime(time1, &start);
  21. parseTime(time2, &end);
  22. // 使用 mktime 将 tm 结构体转换为时间戳
  23. time_t startTime = mktime(&start);
  24. time_t endTime = mktime(&end);
  25. // 计算时间差
  26. return difftime(endTime, startTime);
  27. }
  28. int main() {
  29. // 打卡记录数组
  30. char* punchRecords[] = {
  31. "2022-01-01 07:58:02",
  32. "2022-01-01 12:00:05",
  33. "2022-01-01 16:01:35",
  34. "2022-01-02 00:20:05"
  35. };
  36. int numRecords = sizeof(punchRecords) / sizeof(punchRecords[0]);
  37. // 按照时间顺序对打卡记录进行排序
  38. for (int i = 0; i < numRecords - 1; i++) {
  39. for (int j = 0; j < numRecords - i - 1; j++) {
  40. if (strcmp(punchRecords[j], punchRecords[j + 1]) > 0) {
  41. // 交换记录
  42. char* temp = punchRecords[j];
  43. punchRecords[j] = punchRecords[j + 1];
  44. punchRecords[j + 1] = temp;
  45. }
  46. }
  47. }
  48. // 计算总工作时长
  49. int totalWorkDuration = 0;
  50. for (int i = 0; i < numRecords - 1; i += 2) {
  51. totalWorkDuration += calculateTimeDifference(punchRecords[i], punchRecords[i + 1]);
  52. }
  53. // 输出总工作时长
  54. printf("小蓝在2022年度的总工作时长是%d秒。\n", totalWorkDuration);
  55. return 0;
  56. }

  1. 十四届程序C组试题C
  2. #include <stdio.h>
  3. int main() {
  4. int n; // 事件数量
  5. scanf("%d", &n);
  6. int A[n], B[n], C[n]; // 存储每个事件中的A、B、C值
  7. int maxEvents = -1; // 最多发生的事件数量
  8. int X = 0, Y = 0, Z = 0; // 初始士兵数量
  9. for (int i = 0; i < n; ++i) {
  10. scanf("%d %d %d", &A[i], &B[i], &C[i]);
  11. // 计算每个国家的士兵数量
  12. X += A[i];
  13. Y += B[i];
  14. Z += C[i];
  15. // 判断是否有国家获胜
  16. if ((X > Y + Z) || (Y > X + Z) || (Z > X + Y)) {
  17. // 更新最多发生的事件数量
  18. maxEvents = i + 1;
  19. }
  20. }
  21. printf("%d\n", maxEvents);
  22. return 0;
  23. }

  1. #include <stdio.h>
  2. int maximize_substrings(char* s)
  3. {
  4. int count = 0;
  5. int i, n;
  6. // 遍历字符串,从第二个字符开始
  7. for (i = 1; s[i] != '\0'; ++i)
  8. {
  9. // 如果当前位置是'?',则尽量使其与前一个字符不同
  10. if (s[i] == '?') {
  11. s[i] = (s[i - 1] == '1') ? '0' : '1';
  12. }
  13. // 计算互不重叠的00和11子串的个数
  14. if (s[i] == s[i - 1])
  15. {
  16. count += 1;
  17. }
  18. }
  19. return count;
  20. }
  21. int main()
  22. {
  23. char input_str[] = "1?0?1";
  24. int result = maximize_substrings(input_str);
  25. printf("互不重叠的00和11子串个数:%d\n", result);
  26. return 0;
  27. }

  1. #include <stdio.h>
  2. #include <string.h>
  3. // 函数:计算最小翻转次数
  4. int min_flips_to_match(char S[], char T[])
  5. {
  6. int n = strlen(S);
  7. int flips = 0;
  8. // 从第二个位置到倒数第二个位置进行遍历
  9. for (int i = 1; i < n - 1; ++i)
  10. {
  11. // 如果当前位置的字符与目标串不同
  12. if (S[i] != T[i])
  13. {
  14. // 进行翻转操作
  15. flips++;
  16. S[i] = T[i];
  17. S[i + 1] = (S[i + 1] == '0') ? '1' : '0';
  18. S[i + 2] = (S[i + 2] == '0') ? '1' : '0';
  19. }
  20. }
  21. return flips;
  22. }
  23. int main()
  24. {
  25. // 示例输入
  26. char S[] = "01010";
  27. char T[] = "00000";
  28. // 计算最小翻转次数
  29. int result = min_flips_to_match(S, T);
  30. // 输出结果
  31. printf("Minimum flips required: %d\n", result);
  32. return 0;
  33. }

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MOD 998244353
  4. // 函数:计算矩阵子矩阵价值的和
  5. int matrixSubmatrixSum(int n, int m, int matrix[n][m])
  6. {
  7. // 预处理,计算每个位置的最大值和最小值
  8. int maxVal[n][m];
  9. int minVal[n][m];
  10. for (int i = 0; i < n; ++i)
  11. {
  12. for (int j = 0; j < m; ++j)
  13. {
  14. if (i > 0)
  15. {
  16. maxVal[i][j] = (maxVal[i][j] > maxVal[i - 1][j]) ? maxVal[i][j] : maxVal[i - 1][j];
  17. minVal[i][j] = (minVal[i][j] < minVal[i - 1][j]) ? minVal[i][j] : minVal[i - 1][j];
  18. }
  19. if (j > 0)
  20. {
  21. maxVal[i][j] = (maxVal[i][j] > maxVal[i][j - 1]) ? maxVal[i][j] : maxVal[i][j - 1];
  22. minVal[i][j] = (minVal[i][j] < minVal[i][j - 1]) ? minVal[i][j] : minVal[i][j - 1];
  23. }
  24. maxVal[i][j] = (maxVal[i][j] > matrix[i][j]) ? maxVal[i][j] : matrix[i][j];
  25. minVal[i][j] = (minVal[i][j] < matrix[i][j]) ? minVal[i][j] : matrix[i][j];
  26. }
  27. }
  28. // 计算答案
  29. int result = 0;
  30. for (int a = 1; a <= n; ++a)
  31. {
  32. for (int b = 1; b <= m; ++b)
  33. {
  34. for (int i = 0; i + a - 1 < n; ++i)
  35. {
  36. for (int j = 0; j + b - 1 < m; ++j)
  37. {
  38. int maxInSubmatrix = maxVal[i + a - 1][j + b - 1];
  39. int minInSubmatrix = minVal[i + a - 1][j + b - 1];
  40. result = (result + ((long long)maxInSubmatrix * minInSubmatrix) % MOD) % MOD;
  41. }
  42. }
  43. }
  44. }
  45. return result;
  46. }
  47. int main()
  48. {
  49. // 示例输入
  50. int n = 3, m = 3;
  51. int matrix[3][3] ={{1, 2, 3},{4, 5, 6},{7, 8, 9}};
  52. // 计算答案
  53. int result = matrixSubmatrixSum(n, m, matrix);
  54. // 输出结果
  55. printf("Sum of submatrix values: %d\n", result);
  56. return 0;
  57. }

 

  1. #include <stdio.h>
  2. #include <math.h>
  3. #define MOD 998244353
  4. // 计算欧拉函数
  5. long long phi(long long n) {
  6. long long result = n;
  7. for (long long p = 2; p * p <= n; ++p) {
  8. if (n % p == 0) {
  9. while (n % p == 0)
  10. n /= p;
  11. result -= result / p;
  12. }
  13. }
  14. if (n > 1)
  15. result -= result / n;
  16. return result % MOD;
  17. }
  18. int main() {
  19. long long a, b;
  20. scanf("%lld %lld", &a, &b);
  21. // 计算欧拉函数
  22. long long euler_a = phi(a);
  23. long long euler_ab = phi((long long)pow(a, b));
  24. // 输出结果对MOD取模的值
  25. printf("%lld\n", (euler_ab - euler_a + MOD) % MOD);
  26. return 0;
  27. }

  1. #include <stdio.h>
  2. #define max(a, b) ((a) > (b) ? (a) : (b))
  3. int max_xor_difference(int arr[], int n) {
  4. int prefix_xor_left[100000];
  5. int prefix_xor_right[100000];
  6. // 计算从左往右的前缀异或和
  7. prefix_xor_left[0] = arr[0];
  8. for (int i = 1; i < n; ++i) {
  9. prefix_xor_left[i] = prefix_xor_left[i - 1] ^ arr[i];
  10. }
  11. // 计算从右往左的前缀异或和
  12. prefix_xor_right[n - 1] = arr[n - 1];
  13. for (int i = n - 2; i >= 0; --i) {
  14. prefix_xor_right[i] = prefix_xor_right[i + 1] ^ arr[i];
  15. }
  16. // 计算两个不相交子段内数的异或和的差值的最大值
  17. int max_difference = 0;
  18. for (int i = 0; i < n - 1; ++i) {
  19. max_difference = max(max_difference, max(prefix_xor_left[i], prefix_xor_right[i + 1]));
  20. }
  21. return max_difference;
  22. }
  23. int main() {
  24. int n;
  25. printf("Enter the number of elements in the array: ");
  26. scanf("%d", &n);
  27. int arr[100000];
  28. printf("Enter the array elements: ");
  29. for (int i = 0; i < n; ++i) {
  30. scanf("%d", &arr[i]);
  31. }
  32. int result = max_xor_difference(arr, n);
  33. printf("Maximum XOR difference of two non-overlapping subarrays: %d\n", result);
  34. return 0;
  35. }

  1. #include <stdio.h>
  2. int gcd(int a, int b) {
  3. while (b != 0) {
  4. int temp = b;
  5. b = a % b;
  6. a = temp;
  7. }
  8. return a;
  9. }
  10. void find_numbers(int arr[], int n) {
  11. int i_min = -1, j_min = -1;
  12. for (int i = 0; i < n - 1; ++i) {
  13. for (int j = i + 1; j < n; ++j) {
  14. if (gcd(arr[i], arr[j]) > 1) {
  15. if (i_min == -1 || i < i_min || (i == i_min && j < j_min)) {
  16. i_min = i;
  17. j_min = j;
  18. }
  19. }
  20. }
  21. }
  22. if (i_min == -1) {
  23. printf("No such pair found.\n");
  24. } else {
  25. printf("Pair with minimum i and j: (%d, %d)\n", i_min + 1, j_min + 1);
  26. }
  27. }
  28. int main() {
  29. int n;
  30. printf("Enter the number of elements in the array: ");
  31. scanf("%d", &n);
  32. int arr[1000];
  33. printf("Enter the array elements: ");
  34. for (int i = 0; i < n; ++i) {
  35. scanf("%d", &arr[i]);
  36. }
  37. find_numbers(arr, n);
  38. return 0;
  39. }

  1. #include <stdio.h>
  2. long long calculate_subtree_size(int m, long long k) {
  3. long long subtree_size = 1;
  4. // 计算当前结点的深度
  5. long long depth = 0;
  6. while (k > 0) {
  7. k = (k - 1) / m;
  8. depth++;
  9. }
  10. // 计算子树的结点数量
  11. for (long long i = depth - 1; i > 0; --i) {
  12. subtree_size = subtree_size * m + i;
  13. }
  14. return subtree_size;
  15. }
  16. int main() {
  17. int m;
  18. printf("Enter the degree of the tree (m): ");
  19. scanf("%d", &m);
  20. long long k;
  21. printf("Enter the node number (k): ");
  22. scanf("%lld", &k);
  23. long long result = calculate_subtree_size(m, k);
  24. printf("Number of nodes in the subtree corresponding to node %lld: %lld\n", k, result);
  25. return 0;
  26. }

二、十四届C/C++程序设计B组试题

  1. #include <stdio.h>
  2. #include <string.h>
  3. int main() {
  4. char array[] = "5686916124919823647759503875815861830379270588570991944686338516346707827689565614010094809128502533";
  5. int arraySize = strlen(array);
  6. char date[9]; // 存储提取的8位子序列
  7. int uniqueDates = 0; // 记录不同日期数量
  8. for (int i = 0; i <= arraySize - 8; ++i) {
  9. // 提取8位子序列
  10. strncpy(date, array + i, 8);
  11. date[8] = '\0'; // 添加字符串结束符
  12. // 解析日期
  13. int year, month, day;
  14. sscanf(date, "%4d%2d%2d", &year, &month, &day);
  15. // 检查是否为2023年的合法日期
  16. if (year == 2023 && month >= 1 && month <= 12 && day >= 1 && day <= 31) {
  17. // 记录合法日期,并避免重复计数
  18. uniqueDates++;
  19. i += 7; // 移动到下一个可能的子序列的起始位置
  20. }
  21. }
  22. // 输出结果
  23. printf("满足条件的不同日期数量为:%d\n", uniqueDates);
  24. return 0;
  25. }

  1. #include <stdio.h>
  2. #include <math.h>
  3. // 定义信息熵函数
  4. double entropy_equation(double x) {
  5. double p0 = x / 23333333;
  6. double p1 = (23333333 - x) / 23333333;
  7. double entropy = -(p0 * log2(p0) + p1 * log2(p1));
  8. return entropy - 11625907.5798;
  9. }
  10. // 二分法求解方程
  11. double binary_search() {
  12. double low = 0.0;
  13. double high = 23333333;
  14. double epsilon = 0.000001; // 精度要求
  15. while (high - low > epsilon) {
  16. double mid = (low + high) / 2.0;
  17. double result = entropy_equation(mid);
  18. if (result > 0) {
  19. high = mid;
  20. } else {
  21. low = mid;
  22. }
  23. }
  24. return low;
  25. }
  26. int main() {
  27. double zero_count = binary_search();
  28. printf("0 出现的次数为: %lf\n", zero_count);
  29. return 0;
  30. }

  1. #include <stdio.h>
  2. // 计算最大公约数的欧几里得算法
  3. int gcd(int a, int b) {
  4. if (b == 0) {
  5. return a;
  6. }
  7. return gcd(b, a % b);
  8. }
  9. // 计算数组中所有元素的最大公约数
  10. int findGCD(int arr[], int n) {
  11. int result = arr[0];
  12. for (int i = 1; i < n; i++) {
  13. result = gcd(result, arr[i]);
  14. }
  15. return result;
  16. }
  17. int main() {
  18. int n;
  19. printf("输入冶炼记录的数量 N: ");
  20. scanf("%d", &n);
  21. int A[n], B[n];
  22. printf("输入每条记录中的 A 和 B:\n");
  23. for (int i = 0; i < n; i++) {
  24. scanf("%d %d", &A[i], &B[i]);
  25. }
  26. // 计算最小值
  27. int minV = findGCD(A, n);
  28. // 计算最大值
  29. int maxV = findGCD(B, n);
  30. printf("转换率V的最小值:%d\n", minV);
  31. printf("转换率V的最大值:%d\n", maxV);
  32. return 0;
  33. }

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // 飞机结构体
  4. struct Aircraft
  5. {
  6. int arrivalTime;
  7. int hoverTime;
  8. int landingTime;
  9. };
  10. // 比较函数,用于排序
  11. int compare(const void* a, const void* b)
  12. {
  13. return (((struct Aircraft*)a)->arrivalTime + ((struct Aircraft*)a)->hoverTime) -
  14. (((struct Aircraft*)b)->arrivalTime + ((struct Aircraft*)b)->hoverTime);
  15. }
  16. // 判断是否可以全部安全降落的函数
  17. int canLand(struct Aircraft aircraft[], int N)
  18. {
  19. qsort(aircraft, N, sizeof(struct Aircraft), compare);
  20. int currentTime = 0;
  21. for (int i = 0; i < N; i++)
  22. {
  23. // 判断是否在时间窗口内降落
  24. if (currentTime + aircraft[i].landingTime > aircraft[i].arrivalTime + aircraft[i].hoverTime) {
  25. return 0; // 不能安全降落
  26. }
  27. // 更新当前时间
  28. currentTime = (currentTime + aircraft[i].landingTime > aircraft[i].arrivalTime)
  29. ? currentTime + aircraft[i].landingTime
  30. : aircraft[i].arrivalTime;
  31. }
  32. return 1; // 可以安全降落
  33. }
  34. int main()
  35. {
  36. int N;
  37. printf("请输入飞机数量 N:");
  38. scanf("%d", &N);
  39. struct Aircraft* aircraft = (struct Aircraft*)malloc(N * sizeof(struct Aircraft));
  40. // 输入飞机信息
  41. for (int i = 0; i < N; i++)
  42. {
  43. printf("请输入第 %d 架飞机的到达时刻、盘旋时间和降落时间:", i + 1);
  44. scanf("%d %d %d", &aircraft[i].arrivalTime, &aircraft[i].hoverTime, &aircraft[i].landingTime);
  45. }
  46. // 判断是否可以全部安全降落
  47. if (canLand(aircraft, N))
  48. {
  49. printf("所有飞机可以全部安全降落。\n");
  50. }
  51. else
  52. {
  53. printf("有飞机无法安全降落。\n");
  54. }
  55. free(aircraft);
  56. return 0;
  57. }

  1. #include <stdio.h>
  2. int max(int a, int b) {
  3. return (a > b) ? a : b;
  4. }
  5. int minDeletionsForChain(int arr[], int N)
  6. {
  7. int dp[N];
  8. int result = 0;
  9. for (int i = 0; i < N; i++) {
  10. dp[i] = 1;
  11. for (int j = 0; j < i; j++) {
  12. if (arr[j] % 10 == arr[i] / 10) {
  13. dp[i] = max(dp[i], dp[j] + 1);
  14. }
  15. }
  16. result = max(result, dp[i]);
  17. }
  18. return N - result;
  19. }
  20. int main() {
  21. int N;
  22. printf("请输入数列的长度 N:");
  23. scanf("%d", &N);
  24. int arr[N];
  25. printf("请输入数列 A1, A2, ..., AN:");
  26. for (int i = 0; i < N; i++) {
  27. scanf("%d", &arr[i]);
  28. }
  29. int deletions = minDeletionsForChain(arr, N);
  30. printf("最少需要删除 %d 个数,使剩下的序列是接龙序列。\n", deletions);
  31. return 0;
  32. }

  1. #include <stdio.h>
  2. #define ROW 4
  3. #define COL 5
  4. void dfs(char grid[ROW][COL], int i, int j) {
  5. if (i < 0 || i >= ROW || j < 0 || j >= COL || grid[i][j] == '0') {
  6. return;
  7. }
  8. grid[i][j] = '0'; // 标记当前格子为已访问
  9. // 上下左右四个方向进行深度优先搜索
  10. dfs(grid, i - 1, j);
  11. dfs(grid, i + 1, j);
  12. dfs(grid, i, j - 1);
  13. dfs(grid, i, j + 1);
  14. }
  15. int numIslands(char grid[ROW][COL])
  16. {
  17. int count = 0;
  18. for (int i = 0; i < ROW; ++i) {
  19. for (int j = 0; j < COL; ++j) {
  20. if (grid[i][j] == '1') {
  21. ++count;
  22. dfs(grid, i, j); // 深度优先搜索标记当前岛屿
  23. }
  24. }
  25. }
  26. return count;
  27. }
  28. int main()
  29. {
  30. char grid[ROW][COL] = {
  31. {'1', '1', '0', '0', '0'},
  32. {'1', '1', '0', '0', '0'},
  33. {'0', '0', '1', '0', '0'},
  34. {'0', '0', '0', '1', '1'}
  35. };
  36. int result = numIslands(grid);
  37. printf("岛屿数量: %d\n", result);
  38. return 0;
  39. }

  1. #include <stdio.h>
  2. #include <string.h>
  3. // 计算以c1开头、以c2结尾的子串个数
  4. int countShortenedSubstrings(char S[], char c1, char c2, int K) {
  5. int n = strlen(S);
  6. int count = 0;
  7. for (int i = 0; i < n; ++i) {
  8. if (S[i] == c1) {
  9. for (int j = i + 1; j < n; ++j) {
  10. if (S[j] == c2) {
  11. // 计算子串长度
  12. int length = j - i + 1;
  13. if (length >= K) {
  14. // 符合条件,计数器加一
  15. count++;
  16. }
  17. }
  18. }
  19. }
  20. }
  21. return count;
  22. }
  23. int main() {
  24. char S[100];
  25. char c1, c2;
  26. int K;
  27. // 读取输入
  28. printf("Enter the string S: ");
  29. scanf("%s", S);
  30. printf("Enter the characters c1 and c2: ");
  31. scanf(" %c %c", &c1, &c2);
  32. printf("Enter the value of K: ");
  33. scanf("%d", &K);
  34. // 计算结果并输出
  35. int result = countShortenedSubstrings(S, c1, c2, K);
  36. printf("Number of shortened substrings: %d\n", result);
  37. return 0;
  38. }

  1. #include <stdio.h>
  2. #include <string.h>
  3. // 计算以c1开头、以c2结尾的子串个数
  4. int countShortenedSubstrings(char S[], char c1, char c2, int K) {
  5. int n = strlen(S);
  6. int count = 0;
  7. for (int i = 0; i < n; ++i) {
  8. if (S[i] == c1) {
  9. for (int j = i + 1; j < n; ++j) {
  10. if (S[j] == c2) {
  11. // 计算子串长度
  12. int length = j - i + 1;
  13. if (length >= K) {
  14. // 符合条件,计数器加一
  15. count++;
  16. }
  17. }
  18. }
  19. }
  20. }
  21. return count;
  22. }
  23. int main() {
  24. char S[100];
  25. char c1, c2;
  26. int K;
  27. // 读取输入
  28. printf("Enter the string S: ");
  29. scanf("%s", S);
  30. printf("Enter the characters c1 and c2: ");
  31. scanf(" %c %c", &c1, &c2);
  32. printf("Enter the value of K: ");
  33. scanf("%d", &K);
  34. // 计算结果并输出
  35. int result = countShortenedSubstrings(S, c1, c2, K);
  36. printf("Number of shortened substrings: %d\n", result);
  37. return 0;
  38. }

  1. #include <stdio.h>
  2. #define MAX_NODES 100
  3. int adjacency_list[MAX_NODES][MAX_NODES];
  4. int travel_times[MAX_NODES][MAX_NODES];
  5. int dfs(int node, int parent, int skip_node, int n) {
  6. int total_time = 0;
  7. for (int i = 0; i < n; ++i) {
  8. if (i != parent && adjacency_list[node][i]) {
  9. total_time += dfs(i, node, skip_node, n);
  10. }
  11. }
  12. // 如果当前节点不是要跳过的节点,累加其摆渡车时间
  13. if (node != skip_node) {
  14. total_time += travel_times[node][parent] + travel_times[parent][node];
  15. }
  16. return total_time;
  17. }
  18. int calculate_travel_time(int n, int k, int skip_node) {
  19. int total_time = 0;
  20. for (int i = 1; i < k; ++i) {
  21. total_time += dfs(i, -1, skip_node, n);
  22. }
  23. return total_time;
  24. }
  25. int main() {
  26. int n = 6;
  27. int k = 4;
  28. int skip_node = 2;
  29. // 示例邻接列表和摆渡车时间
  30. int adjacency_list[6][6] = {{0, 1, 1, 0, 0, 0},
  31. {1, 0, 0, 1, 1, 0},
  32. {1, 0, 0, 0, 0, 1},
  33. {0, 1, 0, 0, 0, 0},
  34. {0, 1, 0, 0, 0, 0},
  35. {0, 0, 1, 0, 0, 0}};
  36. int travel_times[6][6] = {{0, 3, 2, 0, 0, 0},
  37. {3, 0, 0, 1, 4, 0},
  38. {2, 0, 0, 0, 0, 5},
  39. {0, 1, 0, 0, 0, 0},
  40. {0, 4, 0, 0, 0, 0},
  41. {0, 0, 5, 0, 0, 0}};
  42. int result = calculate_travel_time(n, k, skip_node);
  43. printf("If skip node %d, total travel time: %d\n", skip_node, result);
  44. return 0;
  45. }

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #define MAX_NODES 1000
  4. // 表示树的邻接列表
  5. int adjacency_list[MAX_NODES][MAX_NODES];
  6. // 标记边是否被访问过
  7. bool visited[MAX_NODES][MAX_NODES];
  8. // 结果数组,记录断开的边的编号
  9. int result[MAX_NODES];
  10. // 当前断开的边的编号
  11. int result_index;
  12. // 深度优先搜索函数
  13. void dfs(int node, int parent, int a, int b, int n) {
  14. for (int i = 0; i < n; ++i) {
  15. if (adjacency_list[node][i] && !visited[node][i]) {
  16. visited[node][i] = visited[i][node] = true;
  17. // 如果当前边的两个端点分别为a和b,说明这条边需要断开
  18. if ((node == a && i == b) || (node == b && i == a)) {
  19. result[result_index++] = i + 1; // 存储边的编号(从1开始)
  20. }
  21. dfs(i, node, a, b, n);
  22. }
  23. }
  24. }
  25. int main() {
  26. int n, m;
  27. scanf("%d %d", &n, &m);
  28. // 初始化邻接列表和visited数组
  29. for (int i = 0; i < n - 1; ++i) {
  30. int u, v;
  31. scanf("%d %d", &u, &v);
  32. adjacency_list[u - 1][v - 1] = adjacency_list[v - 1][u - 1] = 1;
  33. visited[u - 1][v - 1] = visited[v - 1][u - 1] = false;
  34. }
  35. // 遍历每个数对,进行DFS判断是否可以断开
  36. for (int i = 0; i < m; ++i) {
  37. int a, b;
  38. scanf("%d %d", &a, &b);
  39. // 重置结果数组和结果索引
  40. result_index = 0;
  41. // 进行DFS遍历
  42. dfs(0, -1, a - 1, b - 1, n);
  43. // 如果结果索引为0,说明没有找到需要断开的边
  44. if (result_index == 0) {
  45. printf("-1\n");
  46. } else {
  47. // 输出断开的边的编号
  48. for (int j = 0; j < result_index; ++j) {
  49. printf("%d ", result[j]);
  50. }
  51. printf("\n");
  52. }
  53. }
  54. return 0;
  55. }

三、十四届C/C++程序设计A组试题

  1. #include <stdio.h>
  2. // 检查一个数字是否是幸运数字的函数
  3. int is_lucky_number(int num) {
  4. char num_str[20]; // 用于将数字转换为字符串以便处理
  5. sprintf(num_str, "%d", num);
  6. int length = strlen(num_str);
  7. // 如果数字的位数为奇数,则不是幸运数字
  8. if (length % 2 != 0) {
  9. return 0; // 返回假
  10. }
  11. int half_length = length / 2;
  12. int first_half_sum = 0, second_half_sum = 0;
  13. // 计算前半部分数字的和
  14. for (int i = 0; i < half_length; i++) {
  15. first_half_sum += num_str[i] - '0'; // 将字符转换为数字
  16. }
  17. // 计算后半部分数字的和
  18. for (int i = half_length; i < length; i++) {
  19. second_half_sum += num_str[i] - '0';
  20. }
  21. // 判断前半部分和后半部分是否相等
  22. return first_half_sum == second_half_sum;
  23. }
  24. // 计算在给定范围内的幸运数字的数量
  25. int count_lucky_numbers(int start, int end) {
  26. int count = 0;
  27. // 遍历给定范围内的所有数字
  28. for (int num = start; num <= end; num++) {
  29. // 如果是幸运数字,增加计数
  30. if (is_lucky_number(num)) {
  31. count++;
  32. }
  33. }
  34. return count;
  35. }
  36. int main() {
  37. int start_range = 1;
  38. int end_range = 100000000;
  39. // 调用函数计算幸运数字的数量
  40. int result = count_lucky_numbers(start_range, end_range);
  41. // 打印结果
  42. printf("There are %d lucky numbers between %d and %d.\n", result, start_range, end_range);
  43. return 0;
  44. }

  1. #include <stdio.h>
  2. // 定义最大题目数和最大分数
  3. #define MAX_QUESTIONS 30
  4. #define MAX_SCORE 100
  5. // 计算小蓝所有可能的答题情况数量的函数
  6. long long count_possible_ways() {
  7. int target_score = 70;
  8. int num_questions = 30;
  9. // 初始化动态规划数组
  10. long long dp[MAX_QUESTIONS + 1][MAX_SCORE + 1] = { 0 };
  11. dp[0][0] = 1;
  12. // 遍历每一道题目
  13. for (int i = 1; i <= num_questions; i++) {
  14. for (int j = 0; j <= target_score; j++) {
  15. // 如果不答这道题
  16. dp[i][j] += dp[i - 1][j];
  17. // 如果答对这道题,分数增加10
  18. if (j >= 10) {
  19. dp[i][j] += dp[i - 1][j - 10];
  20. }
  21. }
  22. }
  23. return dp[num_questions][target_score];
  24. }
  25. int main() {
  26. // 调用函数计算小蓝所有可能的答题情况数量
  27. long long possible_ways = count_possible_ways();
  28. // 打印结果
  29. printf("小蓝所有可能的答题情况有 %lld 种。\n", possible_ways);
  30. return 0;
  31. }

  1. #include <stdio.h>
  2. #include <math.h>
  3. // 判断是否存在整数y和z,使得x = y^2 - z^2
  4. int has_yz(int x) {
  5. int limit = (int)sqrt(x);
  6. // 遍历y的可能取值
  7. for (int y = 1; y <= limit; y++) {
  8. int z_square = y * y - x;
  9. // 如果z的平方为非负数且是完全平方数
  10. if (z_square >= 0 && sqrt(z_square) == (int)sqrt(z_square)) {
  11. return 1; // 存在满足条件的y和z
  12. }
  13. }
  14. return 0; // 不存在满足条件的y和z
  15. }
  16. // 计算满足条件的x的数量
  17. int count_x(int L, int R) {
  18. int count = 0;
  19. // 遍历给定范围内的所有x
  20. for (int x = L; x <= R; x++) {
  21. // 如果存在整数y和z,使得x = y^2 - z^2
  22. if (has_yz(x)) {
  23. count++;
  24. }
  25. }
  26. return count;
  27. }
  28. int main() {
  29. int L, R;
  30. // 输入L和R的值
  31. printf("Enter the values of L and R: ");
  32. scanf("%d %d", &L, &R);
  33. // 调用函数计算满足条件的x的数量
  34. int result = count_x(L, R);
  35. // 输出结果
  36. printf("There are %d numbers x in the range [%d, %d] satisfying the condition.\n", result, L, R);
  37. return 0;
  38. }

  1. #include <stdio.h>
  2. #include <string.h>
  3. // 计算满足条件的不同子串选择方案的数量
  4. int count_substring_choices(char num_str[]) {
  5. int n = strlen(num_str);
  6. int num = atoi(num_str);
  7. int choices = 0; // 记录不同的选择方案数量
  8. char substring[20]; // 假设数字最大长度为20
  9. // 从左往右遍历,找到第一个递减的位置
  10. for (int i = 0; i < n - 1; i++) {
  11. if (num_str[i] > num_str[i + 1]) {
  12. // 在递减位置及其左侧选择子串
  13. strncpy(substring, num_str, i + 1);
  14. substring[i + 1] = '\0'; // 添加字符串结束符
  15. printf("选择子串: %s\n", substring);
  16. choices++;
  17. }
  18. }
  19. return choices;
  20. }
  21. int main() {
  22. char num_str[20]; // 假设数字最大长度为20
  23. // 输入数字字符串
  24. printf("请输入数字字符串: ");
  25. scanf("%s", num_str);
  26. // 调用函数计算满足条件的不同子串选择方案数量
  27. int result = count_substring_choices(num_str);
  28. // 输出结果
  29. printf("不同的子串选择方案数: %d\n", result);
  30. return 0;
  31. }

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX_COLORS 1001
  4. // 结点的定义
  5. struct TreeNode {
  6. int color;
  7. struct TreeNode* children[1001];
  8. int num_children;
  9. };
  10. // 创建一个新的结点
  11. struct TreeNode* createNode(int color) {
  12. struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
  13. node->color = color;
  14. node->num_children = 0;
  15. return node;
  16. }
  17. // DFS遍历并统计子树颜色个数
  18. void dfs(struct TreeNode* root, int color_count[MAX_COLORS], int* balanced_trees) {
  19. int i;
  20. color_count[root->color]++;
  21. // 递归地处理子结点
  22. for (i = 0; i < root->num_children; i++) {
  23. dfs(root->children[i], color_count, balanced_trees);
  24. }
  25. // 检查当前子树是否是颜色平衡树
  26. int min_count = color_count[root->color];
  27. int max_count = color_count[root->color];
  28. for (i = 0; i < MAX_COLORS; i++) {
  29. if (color_count[i] > 0) {
  30. if (color_count[i] < min_count) min_count = color_count[i];
  31. if (color_count[i] > max_count) max_count = color_count[i];
  32. }
  33. }
  34. if (max_count == min_count) (*balanced_trees)++;
  35. color_count[root->color]--;
  36. }
  37. int main() {
  38. int n, i;
  39. int color_count[MAX_COLORS] = {0}; // 记录每种颜色的个数
  40. int balanced_trees = 0; // 颜色平衡子树的数量
  41. // 读取结点个数
  42. scanf("%d", &n);
  43. struct TreeNode* nodes[n + 1]; // 结点数组
  44. // 初始化结点数组
  45. for (i = 1; i <= n; i++) {
  46. int color;
  47. scanf("%d", &color);
  48. nodes[i] = createNode(color);
  49. }
  50. // 构建树
  51. for (i = 2; i <= n; i++) {
  52. int parent;
  53. scanf("%d", &parent);
  54. nodes[parent]->children[nodes[parent]->num_children++] = nodes[i];
  55. }
  56. // DFS遍历树并统计颜色平衡子树的数量
  57. dfs(nodes[1], color_count, &balanced_trees);
  58. printf("%d\n", balanced_trees);
  59. return 0;
  60. }

  1. #include <stdio.h>
  2. // 定义最大瓜的数量和最大重量
  3. #define MAX_N 100
  4. #define MAX_WEIGHT 1000
  5. // 动态规划函数,返回最少需要劈的瓜的数量
  6. int minimumCutting(int weights[], int n, int targetWeight) {
  7. // 定义动态规划数组
  8. int dp[MAX_N + 1][MAX_WEIGHT + 1] = {0};
  9. // 初始化动态规划数组
  10. dp[0][0] = 1;
  11. // 填充动态规划数组
  12. for (int i = 1; i <= n; i++) {
  13. for (int j = 0; j <= targetWeight; j++) {
  14. dp[i][j] = dp[i - 1][j];
  15. if (j >= weights[i - 1]) {
  16. dp[i][j] |= dp[i - 1][j - weights[i - 1]];
  17. }
  18. }
  19. }
  20. // 如果无法得到总重为targetWeight的瓜,返回-1
  21. if (dp[n][targetWeight] == 0) {
  22. return -1;
  23. }
  24. // 回溯找到最少需要劈的瓜的数量
  25. int cuts = 0;
  26. int currentWeight = targetWeight;
  27. int currentMelon = n;
  28. while (currentWeight > 0 && currentMelon > 0) {
  29. if (dp[currentMelon - 1][currentWeight]) {
  30. currentMelon--;
  31. } else {
  32. currentWeight -= weights[currentMelon - 1];
  33. cuts++;
  34. }
  35. }
  36. return cuts;
  37. }
  38. int main() {
  39. // 示例用法
  40. int weights[] = {2, 3, 5, 7};
  41. int n = sizeof(weights) / sizeof(weights[0]);
  42. int target = 10;
  43. int result = minimumCutting(weights, n, target);
  44. printf("%d\n", result); // 输出 2
  45. return 0;
  46. }

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. // 定义无穷大值
  5. #define INF INT_MAX
  6. // 定义设备和连接的结构体
  7. struct Edge {
  8. int to;
  9. int weight;
  10. };
  11. // 定义图的结构体
  12. struct Graph {
  13. int vertices; // 设备的数量
  14. struct Edge** adjMatrix; // 邻接矩阵表示的图
  15. };
  16. // 初始化图
  17. struct Graph* initializeGraph(int vertices) {
  18. struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
  19. graph->vertices = vertices;
  20. // 为邻接矩阵分配内存
  21. graph->adjMatrix = (struct Edge**)malloc(vertices * sizeof(struct Edge*));
  22. for (int i = 0; i < vertices; ++i) {
  23. graph->adjMatrix[i] = (struct Edge*)malloc(vertices * sizeof(struct Edge));
  24. }
  25. // 初始化邻接矩阵
  26. for (int i = 0; i < vertices; ++i) {
  27. for (int j = 0; j < vertices; ++j) {
  28. graph->adjMatrix[i][j].to = -1; // 表示没有连接
  29. graph->adjMatrix[i][j].weight = INF; // 初始化为无穷大
  30. }
  31. }
  32. return graph;
  33. }
  34. // 添加无向边到图
  35. void addEdge(struct Graph* graph, int from, int to, int weight) {
  36. graph->adjMatrix[from][to].to = to;
  37. graph->adjMatrix[from][to].weight = weight;
  38. // 由于是无向图,需要添加反向边
  39. graph->adjMatrix[to][from].to = from;
  40. graph->adjMatrix[to][from].weight = weight;
  41. }
  42. // Dijkstra算法计算从源节点到所有其他节点的最短路径
  43. void dijkstra(const struct Graph* graph, int source, int* dist) {
  44. int visited[graph->vertices];
  45. // 初始化距离数组和访问数组
  46. for (int i = 0; i < graph->vertices; ++i) {
  47. dist[i] = INF;
  48. visited[i] = 0;
  49. }
  50. dist[source] = 0; // 源节点到自身的距离为0
  51. for (int count = 0; count < graph->vertices - 1; ++count) {
  52. int u = -1;
  53. // 选择未访问的节点中距离最小的节点
  54. for (int i = 0; i < graph->vertices; ++i) {
  55. if (!visited[i] && (u == -1 || dist[i] < dist[u]))
  56. u = i;
  57. }
  58. visited[u] = 1; // 标记节点为已访问
  59. // 更新未访问节点的距离
  60. for (int v = 0; v < graph->vertices; ++v) {
  61. if (!visited[v] && graph->adjMatrix[u][v].to != -1 &&
  62. dist[u] + graph->adjMatrix[u][v].weight < dist[v]) {
  63. dist[v] = dist[u] + graph->adjMatrix[u][v].weight;
  64. }
  65. }
  66. }
  67. }
  68. // 计算两个设备之间的通信稳定性
  69. int calculateStability(const int* dist, int destination) {
  70. if (dist[destination] == INF) {
  71. return -1; // 如果没有路径,则返回-1
  72. }
  73. return dist[destination];
  74. }
  75. int main() {
  76. int n, m;
  77. scanf("%d %d", &n, &m); // 输入设备数量和连接数量
  78. struct Graph* graph = initializeGraph(n);
  79. // 输入物理连接的信息
  80. for (int i = 0; i < m; ++i) {
  81. int u, v, w;
  82. scanf("%d %d %d", &u, &v, &w);
  83. addEdge(graph, u - 1, v - 1, w); // 设备编号从1开始,转换为0开始的索引
  84. }
  85. int q;
  86. scanf("%d", &q); // 查询数量
  87. for (int i = 0; i < q; ++i) {
  88. int x, y;
  89. scanf("%d %d", &x, &y);
  90. int* distFromX = (int*)malloc(n * sizeof(int));
  91. dijkstra(graph, x - 1, distFromX);
  92. int stability = calculateStability(distFromX, y - 1);
  93. printf("%d\n", stability);
  94. free(distFromX);
  95. }
  96. // 释放内存
  97. for (int i = 0; i < n; ++i) {
  98. free(graph->adjMatrix[i]);
  99. }
  100. free(graph->adjMatrix);
  101. free(graph);
  102. return 0;
  103. }

  1. #include <stdio.h>
  2. // 计算数组中每个子段的异或和并求和
  3. int main() {
  4. int n;
  5. scanf("%d", &n); // 输入数组长度
  6. int A[n];
  7. for (int i = 0; i < n; ++i) {
  8. scanf("%d", &A[i]); // 输入数组元素
  9. }
  10. int result = 0;
  11. // 计算每个子段的异或和并求和
  12. for (int i = 0; i < n; ++i) {
  13. int XOR_sum = 0;
  14. for (int j = i; j < n; ++j) {
  15. XOR_sum ^= A[j]; // 计算子段的异或和
  16. result += XOR_sum; // 将每个子段的异或和累加到结果中
  17. }
  18. }
  19. printf("%d\n", result);
  20. return 0;
  21. }

  1. #include <stdio.h>
  2. #define N 4
  3. #define M 4
  4. // 检查当前位置是否合法
  5. int is_valid(int board[N][M], int i, int j, int target_color) {
  6. // 检查是否越界
  7. if (i < 0 || i >= N || j < 0 || j >= M) {
  8. return 0;
  9. }
  10. // 检查当前位置是否已填充
  11. if (board[i][j] != -1) {
  12. return 0;
  13. }
  14. // 统计周围黑色方格的数量
  15. int count_black = 0;
  16. for (int x = i - 1; x <= i + 1; x++) {
  17. for (int y = j - 1; y <= j + 1; y++) {
  18. if (x >= 0 && x < N && y >= 0 && y < M && board[x][y] == 1) {
  19. count_black++;
  20. }
  21. }
  22. }
  23. // 检查是否符合数字约束
  24. return count_black == target_color;
  25. }
  26. // 填充周围的方格
  27. void fill_pixels(int board[N][M], int i, int j, int color) {
  28. if (i < 0 || i >= N || j < 0 || j >= M || board[i][j] != -1) {
  29. return;
  30. }
  31. board[i][j] = color;
  32. // 递归填充周围的方格
  33. fill_pixels(board, i - 1, j, color);
  34. fill_pixels(board, i + 1, j, color);
  35. fill_pixels(board, i, j - 1, color);
  36. fill_pixels(board, i, j + 1, color);
  37. }
  38. // 解决像素放置问题
  39. void solve_puzzle(int board[N][M]) {
  40. for (int i = 0; i < N; i++) {
  41. for (int j = 0; j < M; j++) {
  42. if (board[i][j] != -1) {
  43. // 如果当前方格有数字约束,则进行DFS填充
  44. if (is_valid(board, i, j, board[i][j])) {
  45. fill_pixels(board, i, j, 1);
  46. } else {
  47. fill_pixels(board, i, j, 0);
  48. }
  49. }
  50. }
  51. }
  52. }
  53. int main() {
  54. int initial_board[N][M] = {
  55. {-1, 2, -1, -1},
  56. {-1, -1, 3, -1},
  57. { 4, -1, 0, -1},
  58. {-1, -1, -1, -1}
  59. };
  60. // 解决问题
  61. solve_puzzle(initial_board);
  62. // 打印填充后的棋盘
  63. for (int i = 0; i < N; i++) {
  64. for (int j = 0; j < M; j++) {
  65. printf("%d ", initial_board[i][j]);
  66. }
  67. printf("\n");
  68. }
  69. return 0;
  70. }

  1. #include <stdio.h>
  2. // 函数声明
  3. int min_operations(int n);
  4. int main() {
  5. // 例如,假设有10个硬币
  6. int n = 10;
  7. // 调用函数计算最少需要多少次操作
  8. int result = min_operations(n);
  9. // 打印结果
  10. printf("最少需要 %d 次操作\n", result);
  11. return 0;
  12. }
  13. // 求最少需要多少次操作使所有硬币朝上
  14. int min_operations(int n) {
  15. int count = 0; // 记录操作次数
  16. // 从第2个硬币开始,依次判断每个位置的硬币朝上还是朝下
  17. for (int i = 2; i <= n; i++) {
  18. // 如果当前位置的硬币朝下,则需要翻转
  19. if (n % i == 0) {
  20. count++; // 记录操作次数增加
  21. }
  22. }
  23. return count;
  24. }

四、十三届C/C++程序设计C组试题

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. // 比较函数用于qsort
  5. int compareChars(const void *a, const void *b) {
  6. return (*(char *)a - *(char *)b);
  7. }
  8. int main() {
  9. // 原始字符串
  10. char originalString[] = "WHERETHEREISAWILLTHEREISAWAY";
  11. // 获取字符串长度
  12. int length = strlen(originalString);
  13. // 使用qsort按字母表顺序排序字符串
  14. qsort(originalString, length, sizeof(char), compareChars);
  15. // 输出排序后的字符串
  16. printf("排列之后的字符串:%s\n", originalString);
  17. return 0;
  18. }

  1. #include <stdio.h>
  2. int isInterestingTime(int year, int month, int day, int hour, int minute) {
  3. // 将年份、月日、时分拼接成一个数字
  4. int combinedNumber = year * 1000000 + month * 10000 + day * 100 + hour * 1 + minute * 0.01;
  5. // 统计数字2和数字0的个数
  6. int count2 = 0, count0 = 0;
  7. while (combinedNumber > 0) {
  8. int digit = combinedNumber % 10;
  9. if (digit == 2) {
  10. count2++;
  11. } else if (digit == 0) {
  12. count0++;
  13. }
  14. combinedNumber /= 10;
  15. }
  16. // 检查是否符合条件
  17. return count2 == 3 && count0 == 1;
  18. }
  19. int main() {
  20. int count = 0;
  21. // 遍历年份
  22. for (int year = 1000; year <= 9999; year++) {
  23. // 遍历月份
  24. for (int month = 1; month <= 12; month++) {
  25. // 遍历日期
  26. for (int day = 1; day <= 31; day++) {
  27. // 遍历小时
  28. for (int hour = 0; hour <= 23; hour++) {
  29. // 遍历分钟
  30. for (int minute = 0; minute <= 59; minute++) {
  31. // 检查是否符合条件
  32. if (isInterestingTime(year, month, day, hour, minute)) {
  33. count++;
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }
  40. printf("符合条件的时间总数:%d\n", count);
  41. return 0;
  42. }

  1. #include <stdio.h>
  2. void getPaperSize(const char *paperName, int *width, int *height) {
  3. // 解析纸张名称
  4. char sizeChar;
  5. int foldCount;
  6. sscanf(paperName, "A%c%d", &sizeChar, &foldCount);
  7. // 根据A纸张的尺寸计算大小
  8. *width = 1189;
  9. *height = 841;
  10. for (int i = 0; i < foldCount; i++) {
  11. if (*width > *height) {
  12. *width /= 2;
  13. } else {
  14. *height /= 2;
  15. }
  16. }
  17. }
  18. int main() {
  19. char paperName[10];
  20. printf("输入纸张名称(例如:A0, A1, A2): ");
  21. scanf("%s", paperName);
  22. int width, height;
  23. getPaperSize(paperName, &width, &height);
  24. printf("%s纸张的大小为:%dmm x %dmm\n", paperName, width, height);
  25. return 0;
  26. }

  1. #include <stdio.h>
  2. long long calculate_sum(int arr[], int n) {
  3. long long S = 0;
  4. for (int i = 0; i < n; i++) {
  5. for (int j = i + 1; j < n; j++) {
  6. S += (long long)arr[i] * arr[j];
  7. }
  8. }
  9. return S;
  10. }
  11. int main() {
  12. // 替换这里的实际整数值和数组大小
  13. int numbers[] = {a1, a2, /*...*/, an};
  14. int size = sizeof(numbers) / sizeof(numbers[0]);
  15. long long result = calculate_sum(numbers, size);
  16. printf("The sum is: %lld\n", result);
  17. return 0;
  18. }

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // 计算数字的数位之和
  4. int digitSum(int num) {
  5. int sum = 0;
  6. while (num > 0) {
  7. sum += num % 10;
  8. num /= 10;
  9. }
  10. return sum;
  11. }
  12. // 比较两个数字,按照问题描述中的规则进行比较
  13. int compare(const void *a, const void *b) {
  14. int digitSumA = digitSum(*(int *)a);
  15. int digitSumB = digitSum(*(int *)b);
  16. if (digitSumA != digitSumB) {
  17. return digitSumA - digitSumB;
  18. } else {
  19. return *(int *)a - *(int *)b;
  20. }
  21. }
  22. // 找到排序后的第m个元素
  23. int findMthElement(int n, int m) {
  24. int *numbers = (int *)malloc(n * sizeof(int));
  25. // 初始化数组
  26. for (int i = 0; i < n; i++) {
  27. numbers[i] = i + 1;
  28. }
  29. // 使用qsort进行排序,使用自定义的比较函数
  30. qsort(numbers, n, sizeof(int), compare);
  31. // 排序后的第m个元素
  32. int result = numbers[m - 1];
  33. free(numbers); // 释放动态分配的内存
  34. return result;
  35. }
  36. int main() {
  37. // 替换这里的实际整数值
  38. int n = 100; // 假设排序范围是1到100
  39. int m = 10; // 找排序后的第10个元素
  40. int result = findMthElement(n, m);
  41. printf("The %dth element after sorting is: %d\n", m, result);
  42. return 0;
  43. }

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #define MAX_N 100000
  4. int prefixXor[MAX_N + 1];
  5. // 计算前缀异或数组
  6. void computePrefixXor(int arr[], int n) {
  7. prefixXor[0] = 0;
  8. for (int i = 1; i <= n; i++) {
  9. prefixXor[i] = prefixXor[i - 1] ^ arr[i - 1];
  10. }
  11. }
  12. // 判断是否存在异或等于 x 的两个数
  13. bool hasXorPair(int l, int r, int x) {
  14. return (prefixXor[r] ^ prefixXor[l - 1]) == x;
  15. }
  16. // 查询阶段
  17. bool query(int arr[], int n, int l, int x) {
  18. for (int r = 1; r <= n; r++) {
  19. if (hasXorPair(l, r, x)) {
  20. return true;
  21. }
  22. }
  23. return false;
  24. }
  25. int main() {
  26. int n, m;
  27. scanf("%d %d", &n, &m);
  28. int arr[MAX_N];
  29. for (int i = 0; i < n; i++) {
  30. scanf("%d", &arr[i]);
  31. }
  32. // 预处理阶段
  33. computePrefixXor(arr, n);
  34. // 查询阶段
  35. for (int i = 0; i < m; i++) {
  36. int l, x;
  37. scanf("%d %d", &l, &x);
  38. if (query(arr, n, l, x)) {
  39. printf("YES\n");
  40. } else {
  41. printf("NO\n");
  42. }
  43. }
  44. return 0;
  45. }

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX_LEN 100
  4. // 删除边缘字符的函数
  5. void removeEdgeChars(char s[]) {
  6. int len = strlen(s);
  7. int i, j;
  8. for (i = 0; i < len; i++) {
  9. // 检查当前字符是否为边缘字符
  10. if (i - 1 >= 0 && i + 1 < len && s[i] != s[i - 1] && s[i] != s[i + 1]) {
  11. // 删除当前边缘字符
  12. for (j = i; j < len - 1; j++) {
  13. s[j] = s[j + 1];
  14. }
  15. s[len - 1] = '\0';
  16. len--;
  17. i--; // 回退一步,重新检查当前位置
  18. }
  19. }
  20. }
  21. int main() {
  22. char s[MAX_LEN];
  23. scanf("%s", s);
  24. // 进行24次操作
  25. for (int i = 0; i < 24; i++) {
  26. removeEdgeChars(s);
  27. }
  28. if (strlen(s) == 0) {
  29. printf("EMPTY\n");
  30. } else {
  31. printf("%s\n", s);
  32. }
  33. return 0;
  34. }

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // 比较函数用于排序
  4. int compare(const void *a, const void *b) {
  5. return (*(int *)b - *(int *)a);
  6. }
  7. // 计算查询结果的总和
  8. long long calculateSum(int arr[], int n, int queries[][2], int numQueries) {
  9. long long sum = 0;
  10. // 计算原始数组的和
  11. for (int i = 0; i < n; i++) {
  12. sum += arr[i];
  13. }
  14. // 遍历每个查询范围
  15. for (int i = 0; i < numQueries; i++) {
  16. int L = queries[i][0];
  17. int R = queries[i][1];
  18. // 将查询范围内的元素设置为当前数组中的最大值
  19. for (int j = L - 1; j < R; j++) {
  20. sum += (arr[j] - arr[0]); // 增加差值
  21. }
  22. }
  23. return sum;
  24. }
  25. int main() {
  26. int n, numQueries;
  27. scanf("%d %d", &n, &numQueries);
  28. int arr[n];
  29. for (int i = 0; i < n; i++) {
  30. scanf("%d", &arr[i]);
  31. }
  32. // 对数组进行排序
  33. qsort(arr, n, sizeof(int), compare);
  34. int queries[numQueries][2];
  35. for (int i = 0; i < numQueries; i++) {
  36. scanf("%d %d", &queries[i][0], &queries[i][1]);
  37. }
  38. // 计算查询结果的总和
  39. long long result = calculateSum(arr, n, queries, numQueries);
  40. printf("%lld\n", result);
  41. return 0;
  42. }

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. // 技能结构体
  5. typedef struct {
  6. int A;
  7. int B;
  8. } Skill;
  9. // 比较函数用于排序技能
  10. int compare_skills(const void *a, const void *b) {
  11. return ((Skill*)b)->A - ((Skill*)a)->A;
  12. }
  13. // 计算最大攻击力提升
  14. int max_attack_increase(int N, int M, Skill skills[]) {
  15. // 按攻击力提升排序
  16. qsort(skills, N, sizeof(Skill), compare_skills);
  17. int total_attack_increase = 0;
  18. for (int i = 0; i < N; ++i) {
  19. int max_upgrades = ceil((double)skills[i].A / skills[i].B);
  20. int upgrades = fmin(max_upgrades, M);
  21. total_attack_increase += upgrades * skills[i].A;
  22. M -= upgrades;
  23. }
  24. return total_attack_increase;
  25. }
  26. int main() {
  27. // 示例输入
  28. int N = 3;
  29. int M = 5;
  30. Skill skills[] = {{10, 2}, {7, 1}, {5, 1}};
  31. // 计算最大攻击力提升
  32. int result = max_attack_increase(N, M, skills);
  33. // 输出结果
  34. printf("最多可以提高的攻击力: %d\n", result);
  35. return 0;
  36. }

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // 哈希表节点
  4. typedef struct Node {
  5. int value;
  6. int count;
  7. struct Node* next;
  8. } Node;
  9. // 哈希表
  10. typedef struct {
  11. Node** table;
  12. int size;
  13. } HashMap;
  14. // 初始化哈希表
  15. HashMap* createHashMap(int size) {
  16. HashMap* map = (HashMap*)malloc(sizeof(HashMap));
  17. map->size = size;
  18. map->table = (Node**)malloc(sizeof(Node*) * size);
  19. for (int i = 0; i < size; ++i) {
  20. map->table[i] = NULL;
  21. }
  22. return map;
  23. }
  24. // 插入哈希表节点
  25. void insertHashMap(HashMap* map, int value) {
  26. int index = abs(value) % map->size;
  27. Node* newNode = (Node*)malloc(sizeof(Node));
  28. newNode->value = value;
  29. newNode->count = 1;
  30. newNode->next = map->table[index];
  31. map->table[index] = newNode;
  32. }
  33. // 查询哈希表节点
  34. int queryHashMap(HashMap* map, int value) {
  35. int index = abs(value) % map->size;
  36. Node* current = map->table[index];
  37. while (current != NULL) {
  38. if (current->value == value) {
  39. return current->count;
  40. }
  41. current = current->next;
  42. }
  43. return 0;
  44. }
  45. // 计算区间内出现ki次的数的数量
  46. int countOccurrences(int* prefixSum, int left, int right, HashMap* map, int k) {
  47. if (left > right) {
  48. return 0;
  49. }
  50. int count = 0;
  51. if (left == 0) {
  52. count = queryHashMap(map, prefixSum[right]);
  53. } else {
  54. count = queryHashMap(map, prefixSum[right] - prefixSum[left - 1]);
  55. }
  56. return count == k ? 1 : 0;
  57. }
  58. int main() {
  59. int n = 6;
  60. int A[] = {1, 2, 1, 2, 3, 1};
  61. // 计算前缀和
  62. int* prefixSum = (int*)malloc(sizeof(int) * n);
  63. prefixSum[0] = A[0];
  64. for (int i = 1; i < n; ++i) {
  65. prefixSum[i] = prefixSum[i - 1] + A[i];
  66. }
  67. // 构建哈希表
  68. HashMap* map = createHashMap(n);
  69. // 插入前缀和到哈希表
  70. for (int i = 0; i < n; ++i) {
  71. insertHashMap(map, prefixSum[i]);
  72. }
  73. // 示例查询
  74. int queries[][3] = {{0, 2, 2}, {1, 4, 1}, {2, 5, 1}};
  75. // 处理查询
  76. for (int i = 0; i < 3; ++i) {
  77. int left = queries[i][0];
  78. int right = queries[i][1];
  79. int k = queries[i][2];
  80. int result = countOccurrences(prefixSum, left, right, map, k);
  81. printf("在区间[%d, %d]内出现%d次的数有%d个\n", left, right, k, result);
  82. }
  83. // 释放内存
  84. free(prefixSum);
  85. free(map->table);
  86. free(map);
  87. return 0;
  88. }

五、十三届C/C++程序设计B组试题

六、十三届C/C++程序设计A组试题

七、十二届C/C++程序设计C组试题

八、十二届C/C++程序设计B组试题

九、十二届C/C++程序设计A组试题

十、十一届C/C++程序设计C组试题

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/870299
推荐阅读
相关标签
  

闽ICP备14008679号