蓝桥杯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;
}
相关推荐
程序员杰哥4 小时前
如何做接口测试?
自动化测试·python·测试工具·职场和发展·测试用例·接口测试·postman
RisunJan5 小时前
项目经理面试知识点梳理
面试·职场和发展·word
丰锋ff20 小时前
面试问题合集
面试·职场和发展
颜挺锐20 小时前
如何轻松通过性能测试之第四篇:性能测试项目怎么交付?一文讲透九步实施流程(附面试话术 + 实战清单)
面试·职场和发展
带多刺的玫瑰1 天前
Leecode#15刷题之三数之和
算法·leetcode·职场和发展
shehuiyuelaiyuehao1 天前
算法32,连续数组,前缀和+哈希表
算法·leetcode·职场和发展
YonyouHRSaaS1 天前
AI面试工具怎么选型?2026年企业AI面试系统选型标准是什么?
人工智能·面试·职场和发展·ai面试·ai招聘
RisunJan1 天前
产品经理面试知识点梳理
面试·职场和发展·产品经理
小欣加油1 天前
Leetcode2058 找出临界点之间的最小和最大距离
数据结构·c++·算法·leetcode·职场和发展