#20886: RE 學藝不精 無法理解


s610627@whsh.tc.edu.tw (vkis87)


自己在電腦上測試都可以 提交會RE

#include<iostream>
#include<string>
using namespace std;

int main() {
string str;
int a=-1,k=0;
while(cin>>str) {
for(int i=str.size()-1;i>=str.size()/2;i--) {
a++;
if (str[i]==str[a]) {
k=k+2;
}
}
if (k>=str.size()) {
cout << "yes" << endl;
k=0,a=-1;
str="" ;
}
else {
cout << "no" << endl;
k=0,a=-1;
str="" ;
}
}
}

#22509: Re:RE 學藝不精 無法理解


snakeneedy (蛇~Snake)


核心問題出在判斷式 i>=str.size()/2,可以試著跑看看這段程式碼的結果(自己補上 include 和 main)

string str = "string";
cout << ((-1) >= str.size() ? "true" : "false") << endl;
cout << ((-1) >= (int)str.size() ? "true" : "false") << endl;

細節上,(-1) 是 int 而 str.size() 是 size_t,兩者比較可能會出錯(尤其是負數

也可以思考看看其他比較簡潔的作法