Basic C Language Algorithm 38-formatting and outputting all the final numbers of less than 10000, 3810000
// Example!
/*
========================================================== ======================================
Question: calculate the number of all completions within 10000, count the number, and output it in the following format:
28 = 1 + 2 + 4 + 7 + 14.
Note: The end number is the sum of all the factors except itself, and its value is equal to 1!
For example, all the factors of 28 are: 1, 2, 4, 7, 28. Except 28, the sum of other factors is 28, so 28 is the final number!
========================================================== ======================================
*/
# Include <stdio. h>
# Define N 10000
Int ws (int)
{
Int I = 1;
Int sum = 0;
While (I <a) // does not contain a, because the factor of the number must be smaller than itself!
{
If (a % I = 0)
Sum + = I;
I ++;
}
If (sum =)
Return 1;
Else return 0;
}
Void main ()
{
Int n = 2, I, j, k = 0, a [100], shu = 0;
Printf ("the number of completions within % d is:", N );
While (n <= N)
{
If (ws (n ))
{
For (I = 1; I <n; I ++)
If (n % I = 0)
A [k ++] = I;
Printf ("\ n % d: %-4d = % d", shu + 1, n, a [0]);
For (j = 1; j <k; j ++)
Printf ("+ % d", a [j]);
Printf (". \ n ");
Shu ++;
K = 0;
}
N ++;
}
Printf ("\ n % d in total", shu );
Printf ("\ n ");
}
/*
========================================================== ====================================
Rating:
The difficulty of this question lies in the output format. The final number judgment is a simple problem. You can even use the for loop
Implementation! However, to output the format required by the question, you must first find all its factors and save them in the pre-defined
In the array, output n = a [0] first, and then output the remaining + factors through the for loop, in the output ".".
Note that the output must be one by one, that is, the next n is calculated after the judgment + output + reinitialization. Until n> N
The end of the loop (note that the for loop cannot be used; otherwise, the loop will be output N times again ).
========================================================== ======================================
*/
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.