2017 machine questions-Daffodils, 2017
After waiting for a month, I finally waited for the intern arranged by to go online and record the three questions that I had made, so that I could easily review them later. This is the first question. For the classic daffodils, you must review the question. Do not write the input statement as soon as you encounter a machine question. This question is not required! The idea of daffodils is also classic, and details will be used elsewhere. mark it.
Description: The number of daffodils is an n-digit number (n ≥ 3). The sum of the n power of the number on each digit is equal to itself. (For example, 1-digit + 5-digit + 3-digit = 153). The sum of all daffodils and daffodils within the range of 1000 is output.
Input Description: None
Output description: The total number of all daffodils within 1000.
Output example:
1st daffodils: xxx
2nd daffodils: xxx
3rd daffodils: xxx
...
The sum of daffodils is: xxx
1 public class ShuiXianHua {2 public static void main (String [] args) {3 int count = 0, sum = 0; 4 for (int I = 100; I <1000; I ++) 5 {6 if (judge (I) 7 {8 System. out. println ("no." + (++ count) + "daffodils:" + I); 9 sum = sum + I; 10} 11} 12 System. out. println ("sum of daffodils:" + sum); 13} 14 public static boolean judge (int x) 15 {16 int cpx = x; 17 int n = ("" + x ). length (); // converts a number to a string to calculate the length of a number 18 int s = 0; 19 while (x> 0) // note that while and if cannot be mixed, if it is calculated only once, while does not meet the conditions to jump out of the loop, otherwise the operation is always 20 {21 int m = x % 10; // obtain the remainder: obtain the last digit 22 s + = (int) Math. pow (m, n); // Math. pow is of the double type and needs to be converted to the int type 23 x = x/10; // discard the last digit and continue operation 24} 25 return cpx = s; 26} 27}