题目描述
自守数是指一个数的平方的尾数等于该数自身的自然数。例如:25^2 = 625,76^2 = 5776,9376^2 = 87909376。请求出n以内的自守数的个数
接口说明
/*
功能: 求出n以内的自守数的个数
输入参数:
int n
返回值:
n以内自守数的数量。
*/
public static int CalcAutomorphicNumbers( int n)
{
/*在这里实现功能*/
return 0;
}
输入描述:
int型整数
输出描述:
n以内自守数的数量。
示例1
输入
2000
输出
8
C++实现
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
while (cin>>n)
{
int count = 0;
for (int i = 0; i < n; i++)
{
if ((i%10==0)|| (i % 10 == 1)|| (i % 10 == 5)|| (i % 10 == 6))
{
long temp = i * i;
string stri = to_string(i);
string strt = to_string(temp);
if (strt.find(stri, strt.size() - stri.size())!=-1)
{
count++;
}
}
}
cout<<count<<endl;
}
return 0;
}