题解 | #自守数#--遍历,字符串截取
自守数
https://www.nowcoder.com/practice/88ddd31618f04514ae3a689e83f3ab8e
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
int count = 0;
// 判断是否是自守数
for (int i = 0; i <= num; i++) {
if (i <= 1) {
count++;
} else {
if (isSelfNum(i)) {
count++;
}
}
}
System.out.println(count);
}
private static boolean isSelfNum(int in) {
int tem = in * in;
int len1 = String.valueOf(in).length();
int len2 = String.valueOf(tem).length();
int tail = Integer.parseInt(String.valueOf(tem).substring(len2 - len1, len2));
if (tail == in) {
return true;
}
return false;
}
}

查看25道真题和解析
华为HUAWEI工作强度 1383人发布