Various Combinations of "15 to 5" (Combined Generation Algorithm) are obtained)

Source: Internet
Author: User

Today, a user asked me how to generate the combination number.AlgorithmProblems,This is written early,ForgottenAlmost all,OnlyI replied to him about the principle.This is the time to play"15Select5"I know that it is random, and the probability problem (in the middle is in progress, in the middle is not in progress). I have no good analysis, but I can't help but want to calculate it ......

Okay, no nonsense. We will start the work below. 

I. Principles 

Using the binary counting mechanism,For example, two characters are selected from the string "123", which are calculated as follows:

1: The binary string is
0001

2:
0010

3:
0011
 

4:
0100
 

5:
0101
 

6:
0110
 

7:
0111
 

8:
1000
 

Which contains two 

3:
0011
 

5:
0101
 

6:
0110
 

So there are 3 in total 

If it is 1Current location data, Not 1ThenNot Displayed. After matching with the string "123", the result is: 

23 

1
3
 

12 

That is: 

23 

13 

12 

The method is very good, but it can be used to implement functions.. 

Ii. ExampleCode 

The following is the sample code of C ++: 

View code

 1   /*  
2 File: combination. cpp
3 Author: Mike
4 E-mail: Mike_Zhang@live.com
5 */
6 # Include <cstdio>
7 # Include <cstdlib>
8 # Include <iostream>
9 # Include < String >
10 # Include <vector>
11
12 Using Namespace STD;
13
14 // Calculate the number of 1 in binary data.
15 Int Count (unsigned Int V)
16 {
17 Int Ret = 0 ;
18 While (V> 0 )
19 {
20 V & = (V- 1 );
21 + + Ret;
22 }
23 Return RET;
24 }
25
26 // Checks whether the binary data at the POs position is 1.
27 Bool Check (unsigned Int V, Int Pos)
28 {
29 UnsignedInt TMP = 0x01 ;
30 Int SZ = Sizeof (V )* 8 ;
31 If (Pos> sz)
32 {
33 Printf ( " Pos> SZ \ r \ n " );
34 Return - 1 ;
35 }
36 TMP <= Pos;
37 Return (V & TMP );
38 }
39
40 Int Main ()
41 {
42 Int I = 0 , J = 0 ;
43 Vector < String > Vstr;
44 Int Szvstr = 0 ;
45 String STR = "" ;
46 Int Num = 0 ;
47 Cout < " Input size of vector: " ;
48 Cin> szvstr;
49
50 For (I = 0 ; I <szvstr; ++ I)
51 {
52 Cout < " Input string: " ;
53 Cin> STR;
54 Vstr. push_back (STR );
55 }
56 // Simple data check
57 While (Num> szvstr or num = 0 )
58 {
59 Cout < " Input Number: " ;
60 Cin> num;
61 }
62
63 Unsigned Int Counter =1 ;
64 Counter <= szvstr;
65 Int Szunsign = Sizeof (Unsigned Int )* 8 ;
66
67 Int Total = 0 ;
68 // Output Combination result
69 For (I = 0 ; I <counter; ++ I)
70 {
71 If (Num = count (I ))
72 {
73 For (J = 0 ; J <szunsign & J <szvstr; ++ J)
74 {
75 If (Check (I, j ))
76 Cout <vstr [J] < " " ;
77 }
78 Cout <Endl;
79 ++ Total;
80 }
81 }
82 Cout < " Total = " <Total <Endl;
83 }

Iii. Running Effect 

1And input data: 

First, enter the size of the set, such15 

Enter each element, for example01,02,03...15 

Finally, enter the combination to be selected, for example,"15Select5"Enter5 

The details are as follows: 

2And execution results:

A variety of combinations ("15Select5"And outputs the total number of combinations, as shown below:

 Okay, that's all. I hope it will help you.

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.