Principle of calculating the combination of several numbers

Source: Internet
Author: User

Prepare some questions about the principle of rejection. The principle of Rejection requires any combination of a number factor. For example, 30 = 2*3*5, you need to obtain (2), (3), (5), (2, 3), (2, 5), (3, 5), (2, 3, 5) These combinations can be implemented using dfs. Write one and leave it as a template.
Code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <string. h>
Using namespace std;
 
# Define CLR (arr, val) memset (arr, val, sizeof (arr ))
Int num [4] = {2, 3, 5, 7 };
Int linnum [4];
Int flag [4];
Void dfs (int id, int begin, int cnt ){
If (id = cnt ){
For (int I = 0; I <4; ++ I)
Printf ("% d", linnum [I]);
Printf ("\ n ");
Return;
} Www.2cto.com
For (int I = begin; I <4; ++ I ){
If (! Flag [I]) {
Flag [I] = true;
Linnum [id] = num [I];
Dfs (id + 1, I, cnt );
Flag [I] = false;
}
}
}
Int main (){
For (int I = 1; I <= 4; ++ I ){
CLR (flag, 0 );
CLR (linnum, 0 );
Dfs (0, 0, I );
Printf ("ss \ n ");
}
Return 0;
}

Author: wmn_wmn

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.