输入字符串

发布于 2025-01-23 09:45:30 字数 849 浏览 1 评论 0原文

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

string solve(string s){
    cout << "inside solve";
    for(int i=0; i<s.length(); i++){
        int occur=0;
        while(i+1 < s.length()){
            if(s[i] == s[i+1])
                occur++;
        }
        if(occur%2!=0 && occur%3!=0 && occur%5!=0)
            return "no";
    }
    cout << "inside solve";
    return "yes";
}

int main(){
    ios_base::sync_with_stdio(0);
    //cin.tie(0); cout.tie(0);
    int tc;
    cin >> tc;
    //cout << tc;
    while(tc--){
        string s;
        cin >> s;
        //cout << "Inside main's while";
        cout << solve(s);
    }
    return 0;
}

在输入字符串后,在我的主要功能中,什么都没发生。使用cin&gt;&gt;是否存在一些问题s。同样,当我不征写cin.tie(0)时; cout.tie(0); 循环也不会进入室内。那怎么了?

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

string solve(string s){
    cout << "inside solve";
    for(int i=0; i<s.length(); i++){
        int occur=0;
        while(i+1 < s.length()){
            if(s[i] == s[i+1])
                occur++;
        }
        if(occur%2!=0 && occur%3!=0 && occur%5!=0)
            return "no";
    }
    cout << "inside solve";
    return "yes";
}

int main(){
    ios_base::sync_with_stdio(0);
    //cin.tie(0); cout.tie(0);
    int tc;
    cin >> tc;
    //cout << tc;
    while(tc--){
        string s;
        cin >> s;
        //cout << "Inside main's while";
        cout << solve(s);
    }
    return 0;
}

In my main function after I'm inputting the string nothing is happening. Is there some problem in using cin >> s. Also when I'm uncommenting the line cin.tie(0); cout.tie(0);
It is not going inside while loop too. So what is happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2025-01-30 09:45:30

您在这里有一个无尽的循环:

        while(i+1 < s.length()){
            if(s[i] == s[i+1])
                occur++;
        }

“我”在LOPP中没有修改。循环将永远运行。条件始终是正确的。

使用第二个变量k = i+1,在段循环中进行此操作,然后将s [i]与s [k ++]进行比较。

不要继续与这些所谓的“竞争方面”合作

You have an endless loop here:

        while(i+1 < s.length()){
            if(s[i] == s[i+1])
                occur++;
        }

'i' is not modified in the lopp. The loop will run forever. The condition is always true.

Use a second variable k=i+1, take this in the while loop and compare s[i] with s[k++].

Do not continue to work with these so called 'competition sides'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文