zoukankan      html  css  js  c++  java
  • 华为机试-自守数

    题目描述
    自守数是指一个数的平方的尾数等于该数自身的自然数。例如: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. }  
  • 相关阅读:
    星云精准测试有力提升金融复杂系统的测试能效
    疫情之下,精准测试的智能可信模式正在成为中流砥柱
    星云测试插装编译流程与CI集成
    自动化测试与精准测试的无缝对接
    “静默式”精准测试,让企业零成本完成黑盒测试的升级对接
    精准测试与开源工具Jacoco的覆盖率能力大PK
    【星云测试】Devops微服务架构下具有代码级穿透能力的精准测试
    【星云测试】开发者测试-采用精准测试工具对Spring Boot应用进行测试
    分享我们团队管理的最佳实践——程序员的周报应如何填写
    [原创]基于VueJs的前后端分离框架搭建之完全攻略
  • 原文地址:https://www.cnblogs.com/wwjldm/p/7158663.html
Copyright © 2011-2022 走看看