考研递归题型总结
作者:
歪嘴战神叁叁叁
,
2021-09-12 22:16:13
,
所有人可见
,
阅读 368
1.判断回文字符串
#include<iostream>
using namespace std;
bool f(string str,int l,int r){
if(l == (str.size() / 2))//if(r == (str.size() / 2) - 1) 这两种写法都能过
return true;
else{
char c1 = str[l],c2 = str[r];
if(c1 == c2)
return f(str,l + 1,r - 1);
else
return false;
}
}
int main()
{
string s;
while(cin >> s){
bool f = true;
int n = s.size();
for(int i = 0,j = n - 1;i <= n/2;i ++,j --){
if(s[i] != s[j]){
f = false;
break;
}
}
if(f) cout << "Yes!" << endl;
else cout << "No!" << endl;
}
}