Daffodil number C language

describe:

      Find all "daffodils" between 0 and 999 and output.
     "Daffodil number" refers to a three-digit number, and the sum of the cubes of its digits is exactly equal to the number itself, such as; 153=1+5+3?, then 153 is a "daffodil number".
      In number theory, Narcissistic number, also known as narcissistic number, self-power number, Armstrong number or Armstrong number, refers to an N-digit number whose sum of the N-th power of each number is equal to this number.
For example, 153, 370, 371, and 407 are three-digit daffodil numbers whose cubes sum to this number:
153 = 1^3 + 5^3 + 3^3.
370 = 3^3 + 7^3 + 0^3.
371 = 3^3 + 7^3 + 1^3.
407 = 4^3 + 0^3 + 7^3.
 

Ideas:

     From what is known, we have several properties about the number of daffodils:

1. The number of daffodils is three digits.

2, the sum of the cubes of its various digits is equal to the number. 

3. There are 4 daffodils numbers between 0 and 999, which are 153, 370, 371, and 407 respectively.

        We mainly do it through the operation of remainder and modulo.

        We can follow the meaning of the question, the most important thing is to find the number of each digit, and get the number of each digit by calculating the remainder and modulo. For example, for 12, 12 / 10 == 1, and 12 % 10 == 2. Find the numbers for each digit. The same is true for three-digit daffodils.

        For example, for 153, how do we do it? The immediate thought is that 153 / 100 == 1, we take out its hundreds digit, and then 153% 100 == 53, take out its tens and ones digit, for 53, we are using 10 for the remainder, and the modulo Then take out its tens and ones digit respectively.

        Since the number of daffodils is three, we start directly with 100. When starting from 0 you will find that the result is 6, the reason is that 0 and 1 are also included. The complete code is as follows:

#include<stdio.h>
#include<math.h>

int main(void)
{
    int i = 0;
    int a = 0;//百位
    int b = 0;//十位
    int c = 0;//个位
    int sum = 0; 
    int count = 0;//计算出现水仙花数的次数

    for(i = 100; i < 1000; i++)
    {
        a = i / 100;
        b = i % 100 / 10;
        c = i % 100 % 10;
        sum = pow(a, 3) + pow(b, 3) + pow(c, 3);

        if(i == sum)
        {
            count++;
        }

    }
    
    printf("%d", count);

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324036978&siteId=291194637