2025.8.14考试总结

· · 个人记录

T637642 比较数字

错误原因:题目意思分析不全面 原代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int T;
    cin>>T;
    while(T--){
        string s;
        cin>>s;
        for(int i=0;i<s.size();i++){
            if(s[i]=='0'){
                cout<<'1';
            }else{
                cout<<s[i];
            }
        }
    }
    return 0;
}

改正后的代码:

#include<bits/stdc++.h>
using namespace std;

int main(){
    string s;  // 存储输入字符串
    cin>>s;    // 输入字符串
    int c=0;   // 标记是否遇到第一个'0'

    for(int i=0;i<s.size();i++){
        if(s[i]=='0' && c==0){  // 遇到第一个'0'
            cout<<'1';          // 替换为'1'
            c=1;                // 设置标记
        }
        else if(c==0){          // 未遇到第一个'0'前的字符
            cout<<s[i];         // 原样输出
        }
        else{                   // 遇到第一个'0'后的字符
            cout<<'1';          // 全部替换为'1'
        }
    }
    cout<<endl;  // 输出换行
    return 0;
}

T637627 张的弓

原来的代码是暴力的,还有点错误

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long T;
    cin>>T;
    while(T--){
        long long n,m,k;
        cin>>m>>n>>k;
        if(n==1&&m==1){
            cout<<1<<endl;
            continue;
        }if(m==1){
            cout<<k<<endl;
        }
        bool jl=0;
        long long x=1,y=1;
        while(k--){
            if(jl==0&&y<m){
                y++;
                continue;
            }
            if(jl==1&&y>1){
                y-=1;
                continue;
            }
            if(y==1){
                x++;
                jl=0;
                continue;
            }
            if(y==m){
                x++;
                jl=1;
            }
        }
        cout<<x+y-2<<endl;
    }
    return 0;
} 

这提应该是根据题意和数据推出公式

#include<bits/stdc++.h>
using namespace std;

int main(){
    long long T;  // 测试用例数量
    cin>>T;       // 输入测试用例数

    while(T--){   // 处理每个测试用例
        long long n, m, k;  // 输入参数
        cin>>n>>m>>k;       // 读取n,m,k

        long long y = k/n + 1;  // 计算y坐标

        long long x = 0;    // 初始化x坐标
        if(y%2 == 0){       // 当y为偶数时
            x = n - k%n;    // x坐标计算方式
        }else{              // 当y为奇数时
            x = k%n + 1;    // x坐标计算方式
        }

        cout << x + y - 2 << endl;  // 输出最终计算结果
    }
    return 0;
}

T637602 可可口乐

原来用的是暴力,不知道使用广搜,没拿分 原代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    string zs="cocacola";
    string s;
    cin>>s;
    int ans;
    for(int i=0;i<8;i++){
        if(s[i]!=zs[i]){
            for(int j=0;j<8;j++){
                if(s[j]!=zs[j]&&s[i]==zs[j]&&s[j]==zs[i]){
                    swap(s[i],s[j]);
                    ans++;
                }
            }
        }
    }for(int i=0;i<8;i++){
        if(s[i]!=zs[i]){
            for(int j=0;j<8;j++){
                if(s[j]!=zs[j]&&s[j]==zs[i]){
                    swap(s[i],s[j]);
                    ans++;
                }
            }
        }
    }
    cout<<ans;
    return 0;
} 

正确写法:

#include<bits/stdc++.h>
using namespace std;

// 目标字符串常量
const string TARGET = "cocacola";

// 队列节点结构体
struct Node {
    int step;      // 当前变换步数
    string state;  // 当前字符串状态
};

queue<Node> processQueue;  // BFS处理队列
map<string, bool> visited; // 已访问状态记录

/**
 * 广度优先搜索实现字符串变换
 * 通过交换字符位置逐步逼近目标字符串
 */
void bfs(string start) {
    processQueue.push({0, start});
    visited[start] = true;

    while (!processQueue.empty()) {
        Node current = processQueue.front();
        processQueue.pop();

        // 终止条件:达到目标状态
        if (current.state == TARGET) {
            cout << current.step;
            return;
        }

        // 生成所有可能的交换状态
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                string newState = current.state;
                swap(newState[i], newState[j]);

                // 检查新状态是否已访问
                if (!visited.count(newState)) {
                    visited[newState] = true;
                    processQueue.push({current.step + 1, newState});
                }
            }
        }
    }
}

int main() {
    string input;
    cin >> input;

    // 输入验证
    if (input.length() != 8) {
        cerr << "Error: Input length must be 8" << endl;
        return 1;
    }

    bfs(input);
    return 0;
}

T637561 小梦的守卫

这题应该用前缀和来做

#include <bits/stdc++.h>
using namespace std;
long long n, q, a[200005], d[200005], s[200005], ss[200005];
int main() {
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        d[n - i + 1] = a[i];//求前缀和
        s[i] = s[i - 1] + a[i];
    }
    for (int i = 1; i <= n; i++) {
        ss[i] = ss[i - 1] + d[i];//求后缀和
    }
    while (q--) {
        long long t;
        cin >> t;
        int l = upper_bound(s + 1, s + 1 + n, t) - s - 1;//二分查找找到从左到右可以坚持到t秒的士兵
        int r = upper_bound(ss + 1, ss + 1 + n, t) - ss - 1;//二分查找找到从右到左可以坚持到t秒的士兵
        if (l + r > n) {
            cout << "Zombies ate your brain!\n";
        } else {
            cout << n - l - r <<endl;//输出士兵的数量
        }
    }
    return 0;
}