题解 | #自守数#

自守数

http://www.nowcoder.com/practice/88ddd31618f04514ae3a689e83f3ab8e

例如:25^2 = 625,
76^2 = 5776,
9376^2 = 87909376。
观察自守数,发现 n在n*n的末尾
出现。所以可以用 n*n.endWith(“n”) 来判断是否是自守数
 import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

    Scanner sc= new Scanner(System.in);
     while (sc.hasNext()){
         int cnt=0;
        int x=sc.nextInt();
         for (int i = 0; i <= x; i++) {
             if(isZS(i)){
                 cnt++;
             }
         }
         System.out.println(cnt);
     }

    }
    public  static boolean  isZS(int x){
        int sq=x*x;
        String str=String.valueOf(x);

        return String.valueOf(sq).endsWith(str);
    }
}


全部评论

相关推荐

07-09 18:33
门头沟学院 Java
这么逆天每年都有人去???&nbsp;填多益网申就是大型的服从性测试
鲁大牛:辅导员在群里发了这个公司我就申了一下。网申居然要写当场开摄像头写两篇不少于三百字的作文。太逆天了
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务