Calculate the number of daffodils between 100 and 999, and the number of 100999 daffodils.
Question: print out all the numbers of daffodils. The so-called Number of daffodils refers to a three-digit number. Each digit is equal to the number itself.
For example, 153 is a daffodils because 153 = the power of 1 + the power of 5 + the power of 3.
Public static void main (String [] args) {System. out. println (Arrays. toString (getNarcissisticNumber (100,999);}/*** print out all the daffodils. The so-called daffodils refer to a three-digit number, each of which is equal to the number itself. * For example, 153 is a daffodils because 153 = the power of 1 + the power of 5 + the power of 3. * @ Param start * @ param end * @ return */public static int [] getNarcissisticNumber (int start, int end) {// defines the result array, used to store the number int [] arr = new int [end-start] that complies with the rules. // index is used as the counter to record the number of elements saved in the arr array. int index = 0; // define an array that stores hundreds and tens of digits respectively. int [] args = new int [3]; for (int I = start; I <end; I ++) {args [0] = I/100; args [1] = I % 100/10; args [2] = I % 100% 10; // calculate the cube of each number and // double sum = Math. pow (args [0], 3) + Math. pow (args [1], 3) + Math. pow (args [2], 3 ); int sum = args [0] * args [0] * args [0] + args [1] * args [1] * args [1] + args [2] * args [2] * args [2]; // determine whether each cube is equal to the current number if (sum = I) {arr [index] = I; index ++ ;}} // copy the number of daffodils stored to remove the initial 0 return Arrays. copyOf (arr, index );}
A distorted statement, defining a method, and determining whether a number is a daffodils number
/*** Determine whether the number is a daffodils Number * @ param num into the number * @ return true is a daffodils number; false is not */public static boolean isNarcissisticNumber (int num) {// define an array that stores hundreds and ten digits respectively. int [] args = new int [3]; args [0] = num/100; args [1] = num % 100/10; args [2] = num % 100% 10; // calculate the cube and int sum of numbers = args [0] * args [0] * args [0] + args [1] * args [1] * args [1]] + args [2] * args [2] * args [2]; return sum = num ;}
Call the isNarcissisticNumber (int num) method in the Method
public static void main(String[] args) {for (int i=100; i<=999; i++) {if (isNarcissisticNumber(i)) {System.out.println(i);}}}