回文数判定题解
2026-03-13 16:35:25
发布于:浙江
1阅读
0回复
0点赞
学生版:
#include<bits/stdc++.h>
#include <ostream>
using namespace std;
bool is(int x) {
int sum=0;
int t=x;
while(x) {
sum=sum*10+x%10;
x/=10;
}
if(sum==t) return true;
else return false;
}
int main()
{
int n;
cin>>n;
if(is(n)) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
老师版:
#include <bits/stdc++.h>
using namespace std;
bool isPal(int n)
{
int temp = n;
int reverse = 0;
while (temp != 0)
{
reverse = reverse * 10 + temp % 10;
temp /= 10;
}
if (reverse == n)
return true;
else
return false;
}
int main()
{
int n;
cin >> n;
if (isPal(n))
cout << "Yes";
else
cout << "No";
return 0;
}
简略版:
#include <bits/stdc++.h>
using namespace std;
bool ispal(string s)
{
int n = s.size();
for (int i = 0; i < n / 2; i++)
{
if (s[i] != s[n - i - 1])
{
return false;
}
}
return true;
}
int main()
{
string s;
cin >> s;
if (ispal(s))
cout << "Yes";
else
cout << "No";
return 0;
}
这里空空如也






有帮助,赞一个