自守数是指一个数的平方的尾数等于该数自身的自然数。例如:25^2 = 625,76^2 = 5776,9376^2 = 87909376。请求出n(包括n)以内的自守数的个数
数据范围:
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int count = 0; for(int i = 0; i <= n; i++){ if((i * i +"").endsWith(i+"")){ count++; } } System.out.println(count); } }
import java.util.Scanner; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = br.readLine()) != null) { int n = Integer.parseInt(line); int count = 0; for (int i = 0; i <= n; i++) { // 计算平方 int n2 = i * i; // 转换为字符串 String n2Str = String.valueOf(n2); String iStr = String.valueOf(i); // 从平方结果中截取最后n位,n = i的长度 String sub = n2Str.substring(n2Str.length() - iStr.length()); if (Integer.parseInt(sub) == i) { //System.out.println(i); count++; } } System.out.println(count); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int sum = 0; for (int i = 0; i <= n; i++) { int pow = i * i; if (String.valueOf(pow).endsWith(String.valueOf(i))) { sum++; } } System.out.println(sum); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); int count = 0; for (int j = 0; j <= a; j++) { int c = j * j; if (String.valueOf(c).endsWith(String.valueOf(j))) { count ++; } } System.out.println(count); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case int b = in.nextInt(); int count = 0; for(int i=0;i <=b;i++){ String c = ""+i*i; String d = ""+i; if(c.substring(c.length()-d.length(),c.length()).equals(d)){ count++; } } System.out.println(count); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(),cnt = 0; for(int i = 0;i <= n;i ++ ){ if((i*i+"").endsWith(i+"")) cnt++; } System.out.print(cnt); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * 自守数 */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int count = 0; for (int i = 0; i <= n; i++) { if (check(i)) { count++; } } System.out.println(count); } private static boolean check(int n) { int result = n * n; char[] nChars = Integer.toString(n).toCharArray(); char[] resultChars = Integer.toString(result).toCharArray(); for (int i = nChars.length - 1; i >= 0; i--) { if (nChars[i] != resultChars[(resultChars.length - 1) - ((nChars.length - 1 ) - i)]) { return false; } } return true; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { int n = sc.nextInt(); int count = 0; for (int i = 0; i <= n; i++) { String str = String.valueOf(i * i); String index = String.valueOf(i); // 比较 i的值是否与i*i的最后相同 if (index.equals(str.substring(str.length() - index.length()))) { count++; } } System.out.println(count); } } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int count = 0; for(int i=0; i<=n; i++){ String nPow = String.valueOf((int)Math.pow(i, 2)); // System.out.print(i + " : " + nPow + " "); String nStr = String.valueOf(i); nPow = nPow.substring(nPow.length()-nStr.length(), nPow.length()); // System.out.println(nPow + " " + nStr); if(nStr.equals(nPow)) count++; } System.out.println(count); } }