Cycle-11. Daffodils, cycle-11 daffodils
1/* 2 * Main. c 3 * C11-loop-11. daffodils 4 * Created on: July 30, 2014 5 * Author: Boomkeeper 6 ******* part through ********* 7 */8 9 # include <stdio. h> 10 # include <math. h> 11 12 int N; // N13 14 void isNarcissus (int I) {15 unsigned int sum = 0; 16 int tem = I; 17 // split all I members and calculate their N power sum 18 do {19 sum + = pow (I % 10, N); 20 I = I/10; 21} while (I! = 0); 22 // if the sum of the N power of N digits equals to the number itself, the number of daffodils 23 if (sum = tem) 24 printf ("% d \ n", tem); 25} 26 27 28 int main (void) {29 30 scanf ("% I", & N ); 31 32 int I; 33 for (I = pow (10, N-1); I <pow (10, N); I ++) 34 isNarcissus (I ); 35 36 return 0; 37}
I failed to pass it all again... Sang Xin...
After checking, when I enter 6 or 7, the returned results are very slow. I will optimize the algorithm and add it back!
Question link:
Http://pat.zju.edu.cn/contests/basic-programming/%E5%BE%AA%E7%8E%AF-11
,
,
Java for loop of daffodils
The number of daffodils refers to an n-digit number (n ≥ 3). The sum of the n power of the number on each digit is equal to itself. -- This is its definition, not to mention why
As for the above program, a = I/100, because a is an integer, so the value range is 0-9, and the number on the I hundred digits is retrieved.
; Int B = I % 100/10. Similarly, B is an integer. After I % 100, it is the number of digits and digits. The value range is 0-99, then, extract the number on the number I 10;
Finally, int c = I % 10 is used to obtain a single digit;
According to definition: if (a * a + B * B + c * c = I), the sum of the n power of the number on each bit is equal to its own
In the computer, for loop statements
# Include <stdio. h>
Int main (void)
{
Int I, j, k;
For (I = 1; I <= 9; I ++)
{
For (j = 0; j <= 9; j ++)
{
For (k = 0; k <= 9; k ++)
{
If (100 * I + 10 * j + k = I * I + j * j + k * k)
{
Printf ("% d \ n", I, j, k );
}
}
}
}
Return 0;
}