共回答了17个问题采纳率:94.1%
我写的代码,你可以参考和学习下: /**
* 求自守数,自守数是其平方后尾数等于该数自身的自然数。
* 例如: 25*25=625 76*76=5776
* 找出1-10000之间所有的自守数并输出。
* @param num
*/
public static void getAllSelfNum(int num) {
int i=1;
while (i<=num) {
String s=i+"";
// 提高效率,仅处理尾数为0,1,5,6的数字0*0=0,1*1=1,5*5=25,6*6=36
if (s.endsWith("0")||s.endsWith("1")||s.endsWith("5")||s.endsWith("6")) {
String squareStr=(i*i)+"";
if (squareStr.endsWith(s)) {
System.out.println("Self Number: "+i);
}
}
i++;
}
}调用:getAllSelfNum(10000);
输出结果:Self Number: 1Self Number: 5Self Number: 6Self Number: 25Self Number: 76Self Number: 376Self Number: 625Self Number: 9376
1年前
10