题解 | #对称平方数#
对称平方数
http://www.nowcoder.com/practice/1b79865e2f534db0adba84dca10d6bae
解1
#include<iostream>
using namespace std;
bool check(int x)
{
if(x == 0) return true;
int c = 0, sum = 0, t = x;
while(x != 0)
{
c = x % 10;
sum = sum * 10 + c;
x /= 10;
}
return sum == t;
}
int main()
{
for(int i = 1; i <= 256; i ++)
{
if(check(i * i)) cout << i << endl;
}
return 0;
}
解2
#include<iostream>
#include<stack>
using namespace std;
bool check(int x)
{
stack<int> stk;
if(x == 0) return true;
int s = 0, t = x;
while(x != 0)
{
stk.push(x % 10);
x /= 10;
}
while(!stk.empty())
{
if(t % 10 != stk.top()) return false;
stk.pop();
t /= 10;
}
return true;
}
int main()
{
for(int i = 1; i <= 256; i ++)
{
if(check(i * i)) cout << i << endl;
}
return 0;
}