蓝桥杯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;
}
相关推荐
爱喝矿泉水的猛男9 小时前
非定长滑动窗口(持续更新)
算法·leetcode·职场和发展
六哥探店实录118 小时前
外带服务的温度:藏在包装里的“生活共情力”
经验分享·职场和发展·生活·创业创新·学习方法·新浪微博·餐饮行业
Alfred king20 小时前
面试150 IPO
面试·职场和发展·贪心·数组··排序
Luntu1 天前
哪些人需要用到MBTI职业性格测试?
面试·职场和发展·求职招聘
月阳羊1 天前
【硬件-笔试面试题】硬件/电子工程师,笔试面试题-26,(知识点:硬件电路的调试方法:信号追踪,替换,分段调试)
笔记·嵌入式硬件·面试·职场和发展
ManageEngineITSM1 天前
从混乱到秩序:IT服务管理如何重塑企业运营效率
大数据·人工智能·程序人生·职场和发展·itsm
恣艺2 天前
LeetCode 127:单词接龙
算法·leetcode·职场和发展
dragoooon342 天前
[LeetCode]每日温度
算法·leetcode·职场和发展
Y小葵2 天前
【Practical Business English Oral Scene Interpretation】入职面试No.8~9
笔记·学习·职场和发展
牛客企业服务2 天前
AI面试与传统面试的核心差异解析——AI面试如何提升秋招效率?
java·大数据·人工智能·python·面试·职场和发展·金融