蓝桥杯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;
}
相关推荐
abant29 小时前
leetcode 239 单调队列 需要一些记忆
算法·leetcode·职场和发展
2301_8090494210 小时前
HAL库速成--嵌入式赛道--蓝桥杯速成
职场和发展·蓝桥杯
打瞌睡的朱尤11 小时前
4.1蓝桥杯训练
职场和发展·蓝桥杯
Little At Air11 小时前
LeetCode 30. 串联所有单词的子串 | 困难 C++实现
算法·leetcode·职场和发展
a里啊里啊11 小时前
常见面试题目集合
linux·数据库·c++·面试·职场和发展·操作系统
生信研究猿12 小时前
leetcode 121.买卖股票的最佳时机
算法·leetcode·职场和发展
And_Ii12 小时前
[蓝桥杯 2023 省 A] 更小的数
蓝桥杯
aqiu11111112 小时前
【算法日记 09】蓝桥杯实战:突破整数极限,拥抱“字符串思维”
算法·职场和发展·蓝桥杯
And_Ii12 小时前
[蓝桥杯 2023 省 A] 平方差
蓝桥杯
一轮弯弯的明月2 天前
贝尔数求集合划分方案总数
java·笔记·蓝桥杯·学习心得