I once attended an interview with a company and printed out 1 ~ Combinations of all five numbers between 26 numbers
For example, I want to print 1 ~ Number of combinations of all 5 between 7
Yes
For example, 1234 1235 1236 1237 1245 ..................
Total (7 !) /(4 !) * (3 !) The number of online searches is more complex. My friends wrote a complex one.
I thought for a long time. One morning, the light flashed a simple non-recursive composite number algorithm.
Here, I would like to share with you and find a regression combination number algorithm.
Note:
Select the number of combinations of K in the number of M combin (M, K)
# Define Max 100int array [Max]; // print the number of groups
Void myprinf (INT array [], int K)
{
For (INT I = 0; I <K; I ++)
{
Printf ("% d", array [I]);
}
Printf ("\ n ");
}
Void combin (int m, int K)
...{
Int I;
For (I = 0; I <50; I ++) array [I] = 0;
Int J = 0;
While (j> = 0)
...{
If (array [J] <(m-K + J + 1 ))
...{
I = J;
Array [J] ++;
}
Else
...{
J --;
Continue;
}
For (; I <K-1; I ++)
...{
Array [I + 1] = array [I] + 1;
}
If (I = k-1)
...{
Myprinf (array, k );
Count ++;
}
J = I;
}
}
Algorithm Implementation
Void main ()
...{
Combin (7, 3);/** // 35
For (INT I = 0; I <3; I ++)
Printf ("% d", array [I]);
}