1 ~ 9. All permutation and combination

Source: Internet
Author: User

# Include <iostream>

Using namespace STD;

Bool used [9]; // use cases from 1 to 9 to determine whether the service is used

Int COUNT = 0; // The number of 9*8*7*6*5*4*3*2

Void print (INT, INT );
Void add (Int &, Int, INT );
Void store (bool *);
Void reset (bool *);

Int main (INT argc, char * argv [])
{
Print (0, 8 );

Cout <"Count =" <count <Endl; // number of output results

Return true;
}

Void print (INT num, int N)
{
Int I;
Int firstnum;
Bool store [9];
 
If (n> = 1)
{
For (I = 0; I <9; I ++)
{
If (! Used [I])
{
Store );
Firstnum = num;
Used [I] = true;
Add (Num, I + 1, n );
Print (Num, n-1 );
Reset (store );
Num = firstnum;
}
}
}
Else if (n = 0)
{
For (I = 0; I <9 ;)
{
If (used [I])
{
I ++;
}
Else if (! Used [I])
{
Num + = I + 1;
// Cout <num <Endl; // input the result of the permutation and combination to the screen, and the output is time-consuming.
Count ++;
Break;
}
}
}
}

Void add (Int & num, int integer, int N)
{
Int I;
For (I = 0; I <n; I ++)
{
Integer * = 10;
}
Num + = integer;
}

Void store (bool * store)
{
Int I;
For (I = 0; I <9; I ++)
{
* Store = used [I];
Store ++;
}
}

Void reset (bool * store)
{
Int I;
For (I = 0; I <9; I ++)
{
Used [I] = * store;
Store ++;
}
}

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.