當我嘗試運行此代碼時,得到相反的輸出。請讓我知道,我在做什么錯...
#include<bits/stdc .h>
using namespace std;
bool ps(string s,int l, int r){
if(l>=r)
return true;
if(s[l]!=s[r])
return false;
return ps(s, l,--r);
}
int main(){
string str="naman";
int s=str.size();
bool b=ps(str,0,s);
if(!b){
cout<<"Not a plaindrome";
}
else{
cout<<"A plaindrome";
}
return 0;
}
uj5u.com熱心網友回復:
您向 輸入了錯誤的起始值ps。線
bool b=ps(str,0,s);
應該
bool b=ps(str,0,s - 1);
因為字串中的最后一個字符在索引處string.size() - 1不是string.size()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/374145.html
