题解 | #完全数计算#

自守数

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

import java.io.*;
import java.util.*;

public class Main {
    
    /*************************
    功能: 求出n以内的自守数的个数
    
    输入参数:
    int n
    
    返回值:
    n以内自守数的数量。
    */
    
    public static int CalcAutomorphicNumbers(int n)
    {
        int count = 0, base = 1;
        for (int i = 0; i <= n; i++)
        {
            if (i >= base) {
                base *= 10;
            }
            if (i * i % base == i) {
                count++;
            }
        }
        return count;
    }
    
    public static void main (String [] args) throws IOException {
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

        String line = null;
        while ((line = f.readLine()) != null)
        {
            int n = Integer.parseInt(line);
            int res = CalcAutomorphicNumbers(n);
            out.println(res);
        }
        out.close();
        f.close();
    }
}

全部评论

相关推荐

07-25 10:53
门头沟学院 Java
投递简历一星期多了,一直卡在第一个环节没进展是不是已经没戏了
Peter____G...:其他大厂都过了,甚至在字节实习了,但是莉莉丝简历依旧秒挂
投递莉莉丝游戏等公司10个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-24 18:34
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

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