题解 | #自守数#

自守数

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


全部评论

相关推荐

不愿透露姓名的神秘牛友
08-20 19:41
那一天的Java_J...:简历完全流水账,学生思维很严重,还有很大的优化空间,可以多看看牛客的简历。
点赞 评论 收藏
分享
爱睡觉的冰箱哥:你是我今晚见过的最美的牛客女孩
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

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