蓝桥杯2025年第十六届省赛真题-水质检测

打印 上一主题 下一主题

主题 1880|帖子 1880|积分 5640



C语言代码:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX_LEN 1000000
  4. int main() {
  5.     char a[MAX_LEN + 1], b[MAX_LEN + 1];
  6.     // 使用 scanf 读取字符数组
  7.     scanf("%s", a);
  8.     scanf("%s", b);
  9.     int ans = 0;
  10.     int pre = -1;
  11.     int state = -1;
  12.     int len = strlen(a);
  13.     for (int i = 0; i < len; i++) {
  14.         if (a[i] == '.' && b[i] == '.') continue;
  15.         if (pre != -1) ans += i - pre - 1;
  16.         if (a[i] == '#' && b[i] == '#') state = 3;
  17.         else if (a[i] == '#' && b[i] == '.') {
  18.             if (state == 2) {
  19.                 ans++;
  20.                 state = 3;
  21.             } else state = 1;
  22.         } else if (a[i] == '.' && b[i] == '#') {
  23.             if (state == 1) {
  24.                 ans++;
  25.                 state = 3;
  26.             } else state = 2;
  27.         }
  28.         pre = i;
  29.     }
  30.     // 使用 printf 输出结果
  31.     printf("%d", ans);
  32.     return 0;
  33. }
  34.    
复制代码

C++代码:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main() {
  5.     string a, b;
  6.     // 使用 cin 读取 string 类型变量
  7.     cin >> a >> b;
  8.     int ans = 0;
  9.     int pre = -1;
  10.     int state = -1;
  11.     for (int i = 0; i < a.size(); i++) {
  12.         if (a[i] == '.' && b[i] == '.') continue;
  13.         if (pre != -1) ans += i - pre - 1;
  14.         if (a[i] == '#' && b[i] == '#') state = 3;
  15.         else if (a[i] == '#' && b[i] == '.') {
  16.             if (state == 2) {
  17.                 ans++;
  18.                 state = 3;
  19.             } else state = 1;
  20.         } else if (a[i] == '.' && b[i] == '#') {
  21.             if (state == 1) {
  22.                 ans++;
  23.                 state = 3;
  24.             } else state = 2;
  25.         }
  26.         pre = i;
  27.     }
  28.     // 使用 cout 输出结果
  29.     cout << ans;
  30.     return 0;
  31. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

天津储鑫盛钢材现货供应商

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表