acm回字符串答案错误

来源:百度知道 编辑:UC知道 时间:2024/06/01 11:27:10
#include <iostream>
using namespace std;

int main(){

char cr[100];
bool pd;

while(cin>>cr){
const int num = strlen(cr);

int a = 0;
for(int i=0; i<num/2; i++){
if(cr[i] == cr[num-i-1]){
a++;
}
}
if(a == num/2){
cout<<"Yes!"<<endl;
}else{
cout<<"No!"<<endl;
}
}
return 0;
}

for 循环有错, 谁能保证num为偶数呢?
若num为奇数, if(a==num/2)中条件永不成立.

试试我的:
#include<iostream>
using namespace std;
int main() {

char temp[20];
int i,j,k;

cout << "Input the string:";
cin >> temp;

for ( i=0; i<20 ; i++ )
if ( temp[i] == '\0' )
break;

k = i-1;

for ( j=0; j < (i+1)/2; j++ ) {
if ( temp[j] != temp[k] ) {
cout << "NOT 回文" << endl;
system( "pause" );
return 0;
}
else
k--;
}

cout << "Is 回文" << endl;
system("pause");
return 0;
}