簡體   English   中英

接受字符串的輸入

[英]Taking input of a string

#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;
}

在我輸入字符串后,在我的主要 function 中沒有任何反應。 使用 cin >> s 有什么問題嗎? 另外,當我取消注釋 cin.tie(0); 行時 cout.tie(0); 它也不會進入 while 循環。 那么發生了什么?

你在這里有一個無限循環:

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

'i' 在 lopp 中沒有被修改。 循環將永遠運行。 條件始終為真。

使用第二個變量 k=i+1,將其放入 while 循環並將 s[i] 與 s[k++] 進行比較。

不要繼續與這些所謂的“競爭方”合作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM