在不同的IDE中没有获得相同的输出

时间:2018-05-09 13:42:54

标签: c++ c++14

我在程序中使用相同的代码和相同的输入,但在不同的IDE中获得不同的输出。

问题:https://www.codechef.com/FLMOCK01/problems/LAPIN

我的解决方案:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
    int testCase=0;
    cin>>testCase;
    while(testCase>0){

        int i=0,m=0;
        string str;
        cin.clear();
        cin.sync();
        getline(cin, str);
        string str1;
        string str2;
        if((str.size()%2)==0){
            m=str.size()/2;
            str1.resize(m);
            str2.resize(m);
            for(i=0;i<m;i++){
                str1[i] = str[i];
            }
            for(i=m;i<str.size();i++){
                str2[i-m] = str[i];
            }
        }
        else{
            m=floor(str.size()/2);
            str1.resize(m);
            str2.resize(m);
            for(i=0;i<m;i++){
                str1[i] = str[i];
            }
            for(i=m+1;i<str.size();i++){
                str2[i-m-1] = str[i];
            }
        }
        sort(str1.begin(), str1.end());
        sort(str2.begin(), str2.end());
        if(str1==str2){
            cout<<"YES"<<endl;
        }
        else{
            cout<<"NO"<<endl;
        }

        testCase--;
    }
    return 0;
}

VS Code和code :: blocks中的输出:YES 没有 是 是 没有 NO

enter image description here

CodeChef IDE,xCode,ideone中的输出: 是 是 没有 是 是 没有 enter image description here

1 个答案:

答案 0 :(得分:0)

您的程序似乎(正确地)检查字符串的前半部分和后半部分是否包含相同的字符集(忽略中间的字符)。从这个意义上讲,第一组输出(YES NO YES YES NO NO)似乎是正确的。

如果您添加程序最终的字符串输出,您可以看到出现了什么问题:https://ideone.com/ap422m

if(str1==str2)
    cout<<"YES " << str1 << "|" << str2 <<endl;
else
    cout<<"NO " << str1 << "|" << str2 <<endl;

输出:

YES |
YES ag|ag
NO ab|de
YES or|or
YES xy|xy
NO abb|aab

所以它进行了正确的检查,但是它读取了作为第一个输入结束的初始行。我会在开始时使用getline(cin, str);,就像你以后一样,以防止这种情况发生。