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

C语言代码:

cs 复制代码
#include <stdio.h>
#include <string.h>

#define MAX_LEN 1000000

int main() {
    char a[MAX_LEN + 1], b[MAX_LEN + 1];
    // 使用 scanf 读取字符数组
    scanf("%s", a);
    scanf("%s", b);
    int ans = 0;
    int pre = -1;
    int state = -1;
    int len = strlen(a);
    for (int i = 0; i < len; i++) {
        if (a[i] == '.' && b[i] == '.') continue;
        if (pre != -1) ans += i - pre - 1;

        if (a[i] == '#' && b[i] == '#') state = 3;
        else if (a[i] == '#' && b[i] == '.') {
            if (state == 2) {
                ans++;
                state = 3;
            } else state = 1;
        } else if (a[i] == '.' && b[i] == '#') {
            if (state == 1) {
                ans++;
                state = 3;
            } else state = 2;
        }
        pre = i;
    }
    // 使用 printf 输出结果
    printf("%d", ans);
    return 0;
}
    

C++代码:

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

int main() {
    string a, b;
    // 使用 cin 读取 string 类型变量
    cin >> a >> b;
    int ans = 0;
    int pre = -1;
    int state = -1;
    for (int i = 0; i < a.size(); i++) {
        if (a[i] == '.' && b[i] == '.') continue;
        if (pre != -1) ans += i - pre - 1;

        if (a[i] == '#' && b[i] == '#') state = 3;
        else if (a[i] == '#' && b[i] == '.') {
            if (state == 2) {
                ans++;
                state = 3;
            } else state = 1;
        } else if (a[i] == '.' && b[i] == '#') {
            if (state == 1) {
                ans++;
                state = 3;
            } else state = 2;
        }
        pre = i;
    }
    // 使用 cout 输出结果
    cout << ans;
    return 0;
}
相关推荐
wang__1230023 分钟前
力扣70题解
算法·leetcode·职场和发展
咚咚轩25 分钟前
蓝桥杯14届 数三角
蓝桥杯·stl
_Itachi__1 小时前
LeetCode 热题 100 101. 对称二叉树
算法·leetcode·职场和发展
嗨信奥5 小时前
蓝桥杯青少 图形化编程(Scratch)编程题每日一练——小猫的城堡
青少年编程·蓝桥杯
Kent_J_Truman8 小时前
最长字符串 / STL+BFS
蓝桥杯
wang__123008 小时前
力扣2680题解
算法·leetcode·职场和发展
wen__xvn8 小时前
每日一题洛谷P8615 [蓝桥杯 2014 国 C] 拼接平方数c++
c++·职场和发展·蓝桥杯
竹下为生17 小时前
LeetCode --- 448 周赛
算法·leetcode·职场和发展
Star Curry19 小时前
【读书笔记】《编码:隐匿在计算机软硬件背后的语言》01 逻辑与开关
stm32·单片机·嵌入式硬件·职场和发展·51单片机·学习方法