题解 | #自守数#注意包含0和n

自守数

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

判断是否是自守数 1、算出n的位数count 2、n*n % 10^count == n?true:false

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int sum = 0;
            for(int i=0;i<=n;i++){
                if(automorphicNumbers(i)){
                    sum++;
                }
            }
            System.out.println(sum);
        }
    }
    public static boolean automorphicNumbers(int n) {
        int count = 1;//n的位数,至少是一位
        int temp = n;
        while(temp / 10 != 0){
            count++;
            temp /= 10;
        }
        if((n * n) % (Math.pow(10,count)) == n){
            return true;
        }
        return false;
    }
}
全部评论

相关推荐

合不合适,我自己说了才算
码农索隆:hr:“真执着啊,来我公司当法人吧”
点赞 评论 收藏
分享
点赞 评论 收藏
分享
宇算唯航:目测实缴资本不超100W的小公司
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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