You can see this problem in csdn:
Given an integer array and an integer number, the sum of the numbers in the array is equal to all the combinations of the given integer number.
For example, int A [4] = {1, 2, 5, 10}, int sum = 100
The combination is as follows:
1, 1 ...... (100)/* 100 1 */
2, 2 ...... (50)/* 50 2 */
, 2, 2... ()/* 2 1, 49 2 */
Think of a solution like this:
# Include <stdio. h>
Void output (int OS, int A [], int B [], int N)
{
Int I, flag = 0;
Printf ("");
For (I = 0; I <n; I ++)
{
If (B [I] = 0) continue;
If (flag = 0)
{
Printf ("% 2D * % 2D", a [I], B [I]);
Flag = 1;
}
Else
Printf ("+ % 2D * % 2D", a [I], B [I]);
}
Printf ("= % d", OS );
}
Void calc (int OS, int S, int from, int A [], int N, int B [])
{
Int I;
If (S = 0 ){
Output (OS, A, B, n );
Return;
} Else {
For (I = from; I <n; I ++ ){
If (S> = A [I]) {
B [I] ++;
Calc (OS, S-A [I], I, A, N, B );
B [I] --;
}
}
}
}
Int main ()
{
Int A [4] = {1, 2, 5, 10 };
Int B [10] = {0 };
Int sum = 100;
Calc (sum, sum, 0, A, 4, B );
Return 0;
}