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;
}
}
}