Java Learning Print code examples of daffodils within 1 1000

  • 2021-07-16 02:18:05
  • OfStack

The number of daffodils: The number of daffodils is 3 digits, and the cubic sum of its digits is equal to the 3 digits themselves, for example: 370 = 33 +73 +00; 371 = 33 +73 +13, 370, 371 is the number of daffodils

Note: To determine whether a 3-digit number is a daffodil number, you must first obtain the single digit, 10 digit and 100 digit of this 3-digit number


public class MyTest {
  public static void main(String[] args) {
/*     Print 1-1000 Number of daffodils in 
     Number of daffodils: for example   153=1*1*1+5*5*5+3*3*3*/

    // Definition tag, daffodil number starting value bit 0
    int count =0;
    // To find out 1000 Within the number of daffodils, we must first traverse each number; Because the number of daffodils is 3 The number of digits is so from 100 Begin 
    for (int i = 100; i < 1000; i++) {
      // Fetch a bit 
      int g = i%10;
      // Take out 10 Bit 
      int s = i/10%10;
      // Take out a hundred digits 
      int b = i/100;
      // Determine whether the cube sum on each bit is equal to itself , If yes, print out the number 
      if (g*g*g+s*s*s+b*b*b==i){
        // If it's daffodils, count Plus 1
        count++;
        System.out.println(i);
      }
    }
    System.out.println("1000 The number of daffodils within: "+count);
  }
}

Output:

153
370
371
407

Number of daffodils within 1000: 4


Related articles: