蓝桥杯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;
}
相关推荐
LucianaiB7 小时前
仓颉语言核心技术解析:如何开发高性能服务端应用
职场和发展·高性能·语言·仓颉
夏鹏今天学习了吗21 小时前
【LeetCode热题100(52/100)】课程表
算法·leetcode·职场和发展
on_pluto_1 天前
【基础复习1】ROC 与 AUC:逻辑回归二分类例子
人工智能·机器学习·职场和发展·学习方法·1024程序员节
Java_小白呀1 天前
第十四届蓝桥杯大赛软件赛国赛Java大学C组(部分)
职场和发展·蓝桥杯·1024程序员节
码农多耕地呗1 天前
力扣226.翻转二叉树(java)
算法·leetcode·职场和发展
235161 天前
【MySQL】慢查寻的发现和解决优化(思维导图版)
java·后端·sql·mysql·职场和发展·数据库开发·数据库架构
码农多耕地呗2 天前
力扣543.二叉树的直径(java)(迭代法 and 左右根后序遍历迭代法)
算法·leetcode·职场和发展
Swift社区2 天前
LeetCode 405 - 数字转换为十六进制数
算法·leetcode·职场和发展