标签:
自守数是指一个数的平方的尾数等于该数自身的自然数。例如:252 = 625,762 = 5776,93762 = 87909376。
请求出n以内的自守数的个数
接口说明
/**
* 功能: 求出n以内的自守数的个数
*
* 输入参数:int n
* 返回值:n以内自守数的数量。
*/
public static int calcAutomorphicNumbers(int n) {
/*在这里实现功能*/
return 0;
}
int型整数
n以内自守数的数量。
2000
8
import java.util.Scanner;
/**
* Author: 王俊超
* Date: 2015-12-25 16:51
* All Rights Reserved !!!
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt"));
while (scanner.hasNext()) {
int n = scanner.nextInt();
System.out.println(calcAutomorphicNumbers(n));
}
scanner.close();
}
private static int calcAutomorphicNumbers(int n) {
int result = 0;
for (int i = 0; i <= n; i++) {
if (isAutomorphicNumber(i)) {
result++;
}
}
return result;
}
private static boolean isAutomorphicNumber(int n) {
int s = n * n;
while (n != 0) {
if (s % 10 == n % 10) {
s /= 10;
n /= 10;
} else {
return false;
}
}
return true;
}
}
标签:
原文地址:http://blog.csdn.net/derrantcm/article/details/51404168