您的位置 首页 编程语言

cube-powerful

A number n>0 is called cube-powerful if it is equal to the sum of the cubes of its digits. Write a function named isCubePowerful that returns 1 if its argument is cube-powerful;
otherwise it returns 0.
The function prototype is
int isCubePowerful(int n);
Hint: use modulo 10 arithmetic to get the digits of the number.

 

 

java代码实现

package com.zzy;

/**
 * 更多请关注: http://huamaodashu.com
 * Created by huamaodashu on 31/07/2018.
 */
public class CubePowerful {
    public static void main(String[] args) {

        System.out.println(isCubePowerful(-81));

    }

    public static int isCubePowerful(int n){

        if(n<=0){
            return 0;
        }

        int tmp = n;
        int sum = 0;
        while (n != 0){
            sum = sum+(int) ( Math.pow((double) (n % 10), 3));
            System.out.println("sum="+sum);
            n=n/10;
        }

        if(sum == tmp){
            return 1;
        }else {
            return 0;
        }
    }
}

 

花猫大叔总结了2020年抖音变现玩赚指南-进阶版的电子书 免费获取

链接: https://pan.baidu.com/s/1S7lF9AQAX2pdESlhF9Rl8Q

提取码:u8yv

 

关于花猫大叔短视频创业 作者: hadoopall

热门文章

发表评论

电子邮件地址不会被公开。