题目
自守数是指一个数的平方的尾数等于该数自身的自然数。例如:252 = 625,762 = 5776,93762 = 87909376。请求出n以内的自守数的个数
接口说明
/*
功能: 求出n以内的自守数的个数
输入参数:
int n
返回值:
n以内自守数的数量。
*/
public static int CalcAutomorphicNumbers( int n)
{
/在这里实现功能/
return 0;
}
知识点 查找
运行时间限制 10M
内存限制 128
输入
int型整数
输出
n以内自守数的数量。
样例输入 2000
样例输出 8
=========================================================
一次通过 100分
===========================================================
package package02;
import java.util.Scanner;
//自守数是指一个数的平方的尾数等于该数自身的自然数。例如:252 = 625,762 = 5776,93762 = 87909376。请求出n以内的自守数的个数
//
//接口说明
//
// /*
// 功能: 求出n以内的自守数的个数
//
// 输入参数:
// int n
// 返回值:
// n以内自守数的数量。
// */
//
// public static int CalcAutomorphicNumbers( int n)
// {
// /*在这里实现功能*/
// return 0;
// }
//
//
//知识点 查找
//运行时间限制 10M
//内存限制 128
//输入
//int型整数
//输出
//n以内自守数的数量。
//样例输入 2000
//样例输出 8
public class oj038 {
public int CalcAutomorphicNumbers(int n)
{
int count=0;
for(int i=0;i<=n;i++)
{
if(isAutomorphic(i))
{
count++;
}
}
return count;
}
public boolean isAutomorphic(int n)
{
int m=n*n;
boolean ret=true;
while(n!=0)
{
if(m%10!=n%10)
{
ret=false;break;
}
m/=10;
n/=10;
}
return ret;
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
System.out.print(new oj038().CalcAutomorphicNumbers(n));
}
}