問題描述:
打印所有不超過n(n<256)的,其平方具有對稱性質的數。如11*11=121。
知識點:
- int 轉字符串方法:不是用 char* 而是 用
to_string()
就可以
關於其他 int 轉 string 和 string 轉 int 的方法還有很多,請看:
https://www.cnblogs.com/smile233/p/8379802.html
https://blog.csdn.net/lxj434368832/article/details/78874108
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int count;
string s;
for (int i = 1; i<256; i++)
{
count = 1;
s = to_string(i*i);
int len = s.length();
for (int a = 0; a<len / 2; a++)
{
if (s[a] != s[len - 1 - a])
count = 0;
}
if (!count)
continue;
cout << i << endl;
}
return 0;
}
- 注意反轉整數的寫法:
#include <bits/stdc++.h>
int rev(int n){ //反轉整數 妙不可言!
int new_n=0;
while(n){
new_n = new_n*10+n%10;
n/=10;
}
return new_n;
}
int main(){
int i;
for(i=1;i<256;i++)
if(i*i==rev(i*i))
printf("%d\n",i);
return 0;
}