Example (8.9) Printing Daffodils (1041), 8.91041
| Example (8.9) print Daffodils (1041) |
|
|
| Source |
Computing overview 05 |
| Description |
| Print out all the "Daffodils". The so-called "Daffodils" refers to a three-digit number. The cubes of the numbers are equal to the numbers themselves. For example, 153 is the number of daffodils, because 153 = 1x1x1 + 5x5 + 3x3. |
| About Input |
| No input. |
| About output |
| Output the number of all daffodils. Each row outputs a number of daffodils. |
| Example Input |
None. |
| Example output |
Don't tell everyone the answer :-) |
|
12345678910111213141516171819202122232425262728 |
#include <stdio.h>int main(){int cube[10] = {0, 1, 8, 27, 64, 125, 216, 343, 512, 729};int a, b, c;for(a = 1; a < 10; ++a){for(b = 0; b < 10; ++b){int x = 100*a + 10*b - cube[a] - cube[b];if(x < 0){break;}for(c = 0; c < 10; ++c){if(cube[c] - c == x){printf("%d%d%d\n", a, b, c);}}}}return 0;}
|
|
Print the number of daffodils. Incorrect answer
C = I % 100;
If I = 365 then c = 65
Write a function to print the daffodils
# Include <stdio. h>
Void main ()
{
Int flower (int a, int B, int c );
Int a, B, c, d, e;
For (a = 1; a <10; a ++)
For (B = 0; B <10; B ++)
For (c = 0; c <10; c ++)
{
D = flower (a, B, c );
If (d = 1)
{
E = 100 * a + B * 10 + c;
Printf ("% d", e );
}
}
}
Int flower (int a, int B, int c)
{
Int I, j, k;
I = a * a + B * B + c * c; // The problem here should be the cubic of each number, that is, the cubic power, not the quadratic side.
J = 100 * a + 10 * B + c;
If (I = j) k = 1;
Else k = 0;
Return k;
}