题目描述
自守数是指一个数的平方的尾数等于该数自身的自然数。例如:25^2 = 625,76^2 = 5776,9376^2 = 87909376。请求出n以内的自守数的个数
接口说明
/*
功能: 求出n以内的自守数的个数
输入参数:
int n
返回值:
n以内自守数的数量。
*/
public static int CalcAutomorphicNumbers( int n)
{
/*在这里实现功能*/
return 0;
}
输入描述:
int型整数
输出描述:
n以内自守数的数量。
#include <iostream>
#include <string>
using namespace std;
//转换成字符串 查找,利用现成的库函数 =-=
int main()
{
long n;
while(cin>>n){
int ans = 2;// 0, 1也是
for(long i = 3; i<=n; i++){
long n2 = i*i;
string s1 = to_string(i);
string s2 = to_string(n2);
int pos = s2.size()- s1.size();
if(s2.find(s1,pos)!=string::npos)
ans++;
}
cout<<ans<<endl;
}
return 0;
}