Combined number Algorithm Implementation

Source: Internet
Author: User

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]);
}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.