题解 | #自守数#
自守数
https://www.nowcoder.com/practice/88ddd31618f04514ae3a689e83f3ab8e
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String a; try { a = in.readLine(); in.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } int n = parse(a); System.out.print(count(n)); } static int parse(String a) { int i = 0, l = a.length(), n = 0; char[] chrAy = new char[l]; a.getChars(0, l, chrAy, 0); while (i < l) { if ((chrAy[i] - '0' | '9' - chrAy[i]) > 0) { n *= 10; n += chrAy[i] - '0'; } i++; } return n; } static int count(int n) { int i = 0, j = 1, tmp = 0, selfDefend = 0; n++; while (i < n) { for (; j < i + 1; j *= 10) { } tmp = i * i; tmp -= i; if (tmp % j == 0) selfDefend++; i++; } return selfDefend; } }