• 华为机试-自守数


    题目描述
    自守数是指一个数的平方的尾数等于该数自身的自然数。例如:25^2 = 625,76^2 = 5776,9376^2 = 87909376。请求出n以内的自守数的个数

    接口说明

    /*
    功能: 求出n以内的自守数的个数

    输入参数:
    int n
    返回值:
    n以内自守数的数量。
    */

    public static int CalcAutomorphicNumbers( int n)
    {
    /*在这里实现功能*/
    return 0;
    }


    输入描述:
    int型整数
    输出描述:
    n以内自守数的数量。
    示例1
    输入

    2000
    输出

    8

    程序实现:

    1. import java.util.Scanner;  
    2.   
    3. /** 
    4.  * 题目描述 自守数是指一个数的平方的尾数等于该数自身的自然数。例如:25^2 = 625,76^2 = 5776,9376^2 = 
    5.  * 87909376。请求出n以内的自守数的个数 
    6.  *  
    7.  * 接口说明 
    8.  *  
    9.  * /* 功能: 求出n以内的自守数的个数 
    10.  *  
    11.  * 输入参数: int n 返回值: n以内自守数的数量。 
    12.  *  
    13.  * 输入描述: int型整数 输出描述: n以内自守数的数量。 示例1 输入 
    14.  *  
    15.  * 2000 输出 
    16.  *  
    17.  * 8 
    18.  */  
    19. public class Main {  
    20.   
    21.     public static void main(String[] args) {  
    22.         Scanner scanner = new Scanner(System.in);  
    23.         while (scanner.hasNext()) {  
    24.             int num = scanner.nextInt();  
    25.             int sum = 0;  
    26.             for (int i = 0; i <= num; i++) {  
    27.                 if (i == CalcAutomorphicNumbers(i)) {  
    28.                     // System.out.println(i);  
    29.                     sum++;  
    30.                 }  
    31.             }  
    32.   
    33.             System.out.println(sum);  
    34.   
    35.         }  
    36.   
    37.     }  
    38.   
    39.     public static int CalcAutomorphicNumbers(int n) {  
    40.         String string = String.valueOf(n);  
    41.         int len = string.length();  
    42.         int wei = (int) Math.pow(10, len);  
    43.         int savewei = wei;  
    44.         int ceng = 1;  
    45.         int current = 0;  
    46.         int n2 = n;  
    47.         int sum = 0;  
    48.         for (int i = 0; i < len; i++) {  
    49.             current = n2 % 10;  
    50.             sum += (current * n % wei) * ceng;  
    51.             n2 = n2 / 10;  
    52.             ceng = ceng * 10;  
    53.             wei = wei / 10;  
    54.         }  
    55.         return sum % savewei;  
    56.     }  
    57.   
    58. }  
  • 相关阅读:
    python3 urllib 类
    python 金角大王博客园学习地址
    配置文件一mapper.xml
    配置文件一mybatis-config.xml
    配置文件一applicationContext.xml
    类文件路径一classpath
    配置文件一web.xml
    日志框架一logback配置和使用
    SpringBoot + redis实现分布式session共享
    SpringBoot集成Redis 一 分布式锁 与 缓存
  • 原文地址:https://www.cnblogs.com/wwjldm/p/7158663.html
Copyright © 2020-2023  润新知