问题 D: 自守数
题目描述
自守数是指一个数的平方的尾数等于该数自身的自然数。例如:25^2 = 625,76^2 = 5776,9376^2 = 87909376。请求出n以内的自守数的个数。
输入
int型整数。
输出
n以内自守数的数量。
比华为的OJ数据加强了,而且还卡时间了,由于本人太菜,错了几十次
错了几十次,发现,一:它平方后,会溢出(C++用long long,java用long),二:超时,解决了溢出,还超时,把java的Int范围内的自守数打表出来,过很久才结束,所以,我放弃了,还是这样去做吧,如果有更好的方法请在下方评论,谢谢
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long T;
Map<Integer, Integer> mp = new TreeMap<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});
mp.put(0, 1);
mp.put(1, 2);
mp.put(5, 3);
mp.put(6, 4);
mp.put(25, 5);
mp.put(76, 6);
mp.put(376, 7);
mp.put(625, 8);
mp.put(9376, 9);
mp.put(90625, 10);
mp.put(109376, 11);
mp.put(890625, 12);
mp.put(2890625, 13);
mp.put(7109376, 14);
mp.put(12890625, 15);
mp.put(87109376, 16);
mp.put(212890625, 17);
mp.put(787109376, 18);
mp.put(1787109376, 19);
while (sc.hasNext()) {
T = sc.nextInt();
for (Map.Entry<Integer, Integer> m : mp.entrySet()) {
// System.out.println(m.getKey()+" "+m.getValue());
if (T >= m.getKey()) {
System.out.println(m.getValue());
break;
}
}
}
}
}
版权声明:本文为qq_43520913原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。