24 lines
533 B
C++
24 lines
533 B
C++
|
// 输入一个数,判断它是否为回文数,即正着读和倒着读都一样
|
||
|
#include <iostream>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
int n, temp, res;
|
||
|
cout << "请输入一个数:";
|
||
|
cin >> n;
|
||
|
temp = n; // 拷贝一份 n
|
||
|
res = 0;
|
||
|
|
||
|
// 原理:将 n 从个位开始取出,然后乘以 10 加到 res 中
|
||
|
for (; temp > 0; temp /= 10)
|
||
|
res = res * 10 + temp % 10;
|
||
|
|
||
|
if (res == n)
|
||
|
cout << "是回文数" << endl;
|
||
|
else
|
||
|
cout << "不是回文数" << endl;
|
||
|
|
||
|
return 0;
|
||
|
}
|