题解 | #自守数#

自守数

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);
    }
}


全部评论

相关推荐

09-14 17:23
门头沟学院
故事和酒66:所以说副业很重要,程序员干到40岁,再怎么也赚300万了,吃吃利息也够活下去
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

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