• 立方变自身


    观察下面的现象,某个数字的立方,按位累加仍然等于自身。

    1^3 = 1 
    8^3  = 512    5+1+2=8
    17^3 = 4913   4+9+1+3=17
    ...
    

    请你计算包括1,8,17在内,符合这个性质的正整数一共有多少个?
    请填写该数字,不要填写任何多余的内容或说明性的文字。 答案:

    显然是有个范围的,一开始直接把范围设大,结果是6.

    但是仔细分析,n位数各位和最大是9n,n位数的三次方最多3n位,各位和最大27n,n位数最小是10^(n-1),令27n<10^(n-1),显然n>=3就已经不满足了,n最大是2,那么n为2时,27n=54,所以范围再次缩小。

    代码:

    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
        private static Scanner sc = new Scanner(System.in);
        private static BigInteger pow3(BigInteger a) {
            return a.multiply(a.multiply(a));
        }
        private static BigInteger all(BigInteger a) {
            BigInteger sum = BigInteger.ZERO;
            while(a.compareTo(BigInteger.ZERO) != 0) {
                sum = sum.add(a.mod(BigInteger.TEN));
                a = a.divide(BigInteger.TEN);
            }
            return sum;
        }
        public static void main(String[] args) {
            BigInteger a = BigInteger.ONE;
            int c = 0;
            while(a.bitCount() <= 11) {
                if(a.compareTo(all(pow3(a))) == 0) c ++;
                a = a.add(BigInteger.ONE);
            }
            System.out.println(c);
        }
    }

    代码:

    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
        private static Scanner sc = new Scanner(System.in);
        private static int all(int a) {
            int sum = 0;
            while(a != 0) {
                sum += a % 10;
                a /= 10;
            }
            return sum;
        }
        public static void main(String[] args) {
            int c = 0;
            for(int i = 1;i <= 54;i ++) {
                if(i == all(i * i * i)) c ++;
            }
            System.out.println(c);
        }
    }

    代码:

  • 相关阅读:
    Centos7部署jenkins
    查看杀死django进程
    【填坑】python3 manage.py migrate:?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
    Amazon S3 加密
    向 Elastic Beanstalk 环境中添加数据库
    Amazon RDS 监控概览
    AWS Lambda 环境变量
    使用 DynamoDB Auto Scaling 自动管理吞吐容量
    使用 Amazon Aurora Auto Scaling 扩展 Aurora 副本
    ECS 服务 Auto Scaling 和 Application Auto Scaling
  • 原文地址:https://www.cnblogs.com/8023spz/p/10536360.html
Copyright © 2020-2023  润新知