蓝桥杯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;
}
相关推荐
程序员三藏17 分钟前
Selenium+python自动化测试:解决无法启动IE浏览器及报错问题
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
黑客影儿5 小时前
黑客哲学之学习笔记系列(三)
笔记·学习·程序人生·安全·职场和发展·网络攻击模型·学习方法
熬了夜的程序员5 小时前
【LeetCode】16. 最接近的三数之和
数据结构·算法·leetcode·职场和发展·深度优先
Miraitowa_cheems5 小时前
LeetCode算法日记 - Day 15: 和为 K 的子数组、和可被 K 整除的子数组
java·数据结构·算法·leetcode·职场和发展·哈希算法
火车叨位去19492 天前
力扣top100(day04-05)--堆
算法·leetcode·职场和发展
qq_513970442 天前
力扣 hot100 Day76
算法·leetcode·职场和发展
程序员曦曦3 天前
10:00开始面试,10:06就出来了,问的问题有点变态。。。
自动化测试·软件测试·功能测试·程序人生·面试·职场和发展
是乐谷3 天前
阿里云杭州 AI 产品法务岗位信息分享(2025 年 8 月)
java·人工智能·阿里云·面试·职场和发展·机器人·云计算
天才测试猿3 天前
常见的Jmeter压测问题
自动化测试·软件测试·python·测试工具·jmeter·职场和发展·压力测试
墨染点香3 天前
LeetCode 刷题【43. 字符串相乘】
算法·leetcode·职场和发展