你首|

2c237c6

园龄:1年4个月粉丝:2关注:0

解答旋转词问题bug改造过程

大家先看代码:

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

int main(){
    int a,b;
    string c,d,count=1;
    cin >> a >> b;
        cin >> c;
        cin >> d;
         if(a!=b){
        cout << "No";
    }else{
        string str= c+c;
        for(int i=0;i<a;i++){
            string str2 = str.substr(i,a);
            if(str2==d){
                cout<<"YES";//为什么这个明明赋值为一可是并没有等于一
            }else{
                count=0;
            }
        }
        if(count==0){
            cout << "No";
        }
    }
    return 0;
}

这段代码中的问题在于 count 初始化时赋值为字符串 “1”,而不是整数 1。这意味着在后续的比较中,count 与整数进行比较时会发生类型错误。
在这里插入图片描述

正确的做法应该是将 count 声明为整数,并将初始化时的值设置为 1。即:

int count = 1;

此外,代码中应该添加一些必要的注释,使得代码更加易读易懂。同时还需要对代码进行测试和调试,以确保其能够正确运行并处理各种不同的输入情况。

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

int main(){
    int a,b,count=0;
    string c,d;
    cin >> a >> b;
        cin >> c;
        cin >> d;
         if(a!=b){
        cout << "No";
    }else{
        string str= c+c;
        for(int i=0;i<a;i++){
            string str2 = str.substr(i,a);
            if(str2==d){
                cout<<"YES";//为什么这个明明赋值为一可是并没有等于一
            }else{
                count++;
            }
        }
        if(count!=0){
            cout << "No";
        }
    }
    return 0;
}

这个明明count++了为什么跳出if还是变成0最后还是会执行

if(count!=0){
cout << “No”;
}

其实在我的代码中,虽然在循环中 count++count 的值进行了累加,但是我在判断 count 是否为 0 的条件上出现了问题。

在原始代码中,使用 if(count!=0) 判断 count 是否不等于 0,如果成立,则输出 "No"。这是一个逻辑上的误解。

实际上,在这个问题中,如果 count!=0,表示在循环中找到了至少一个位置开始的子串与目标字串不相等。那么我们可以在循环结束后输出 "No",因为这意味着无法找到符合条件的子串。

所以判断条件应该修改为 if(count==a)。如果 count 等于输入的字符串长度 a,则输出 "No"。这样才能正确地判断是否找到了符合条件的子串。

修改后的代码如下:

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

int main(){
    int a, b, count = 0;
    string c, d;
    cin >> a >> b;
    cin >> c;
    cin >> d;
    
    if(a != b){
        cout << "No";
    }else{
        string str = c + c;
        for(int i = 0; i < a; i++){
            string str2 = str.substr(i, a);
            if(str2 == d){
                cout << "YES";
                return 0;  // 找到符合条件的子串,直接退出程序
            }else{
                count++;
            }
        }
        
        if(count == a){
            cout << "No";
        }
    }
    return 0;
}

这个明明count++了为什么跳出if还是变成0最后还是会执行
if(count!=0){
cout << “No”;
}
这样修改后,当找到符合条件的子串时会输出 "YES" 并退出程序,否则会输出 "No"

其实也可以这样

#include<bits/stdc++.h>//-旋转词问题
using namespace std;

int main(){
    int a,b,count=0;
    string c,d;
    cin >> a >> b;
        cin >> c;
        cin >> d;
         if(a!=b){
        cout << "NO";
    }else{
        string str= c+c;
        for(int i=0;i<a;i++){
            string str2 = str.substr(i,a);
            if(str2==d){
                cout<<"YES";//为什么这个明明赋值为一可是并没有等于一
            }else{
                count++;
            }
        }
        if(count==0){//不能数组count!=0会出现逻辑问题
            cout << "NO";
        }
    }
    return 0;
}

本文作者:2c237c6

本文链接:https://www.cnblogs.com/27dCnc/p/18568688

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   2c237c6  阅读(3)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起