Arrangement and combination Summary: Implementation Method for outputting results

Source: Internet
Author: User

Output in full order:

Solution 1:

Copy codeThe Code is as follows: <SPAN style = "COLOR: #333333" >#include <stdio. h>

/*
Recursive thinking:
Take out the first element of the array and put it into the last element, that is, a [0] And a [n] exchange, and then recursively arrange all the elements of a [n ].
1 If the array has only one element n = 1 a = {1}, the entire array is {1}
2. If two elements n = 2 a = {1, 2} exist, the entire array is
After switching between {2, 1} a [1] and a [2], the full arrangement of a [2-1] = {2} is calculated as 1.
After switching between {1, 2} a [2] and a [2], the entire arrangement of a [2-1] = {1} is obtained.
3 if there are three elements n = 3 a = {1, 2, 3 },
After switching between {2, 3}, 1} a [1] and a [3], the complete arrangement of a [3-1] = {2, 3} is summed up to 2.
{1, 3}, 2} a [2] a [3] After exchange, the full arrangement of a [3-1] = {1, 3} is obtained. 2
{1, 2}, 3} a [3] a [3] After an exchange, the full arrangement of a [3-1] = {1, 2} is obtained.
*/
Void swap (int a [], int size)
{
Int I, t;
If (size = 0)
{
For (I = 0; I <5; I ++)
{
Printf ("% c", a [I]);
}
Printf ("\ n ");
Return;
}
Else
{
For (I = 0; I <= size; I ++) // all the elements in the loop
{
// Before writing it in swap (), recursively passing the value to the exit point should be passed into the function
T = a [I]; a [I] = a [size]; a [size] = t;
Swap (a, size-1 );
// What is the starting point of array element restoration? after changing the current position or position, the original position is changed.
// Easy to switch from a [1] to a [n] to the last element
T = a [I]; a [I] = a [size]; a [size] = t;

}
}
}
Int main ()
{
Int a [5], I;
For (I = 0; I <5; I ++)
{
A [I] = 97 + I;
}
Swap (a, 4 );
// Printf ("\ n % d", m );
Return 0;
} </SPAN>

Solution 2:

Copy codeThe Code is as follows: <SPAN style = "COLOR: #333333" >#include <stdio. h>
// Obtain the full arrangement of the remaining numbers starting with 1 2 3 4 5 until they are simplified to a number.
Void swap (int a [], int k)
{
Int I, m, t = 0;
If (k = 5)
{
For (I = 0; I <5; I ++)
{
Printf ("% d", a [I]);
}
// K ++;
Printf ("\ n ");
}
For (I = k; I <5; I ++)
{
{M = a [k]; a [k] = a [I]; a [I] = m ;}
Swap (a, k + 1 );
{M = a [k]; a [k] = a [I]; a [I] = m ;}
}
}

Int main ()
{
Int a [5] = {1, 2, 3, 4, 5}; // Number of recursive operations
Swap (a, 0); // function call
Return 0;
}
</SPAN>

N of the m numbers are arranged:

Copy codeThe Code is as follows: # include <stdio. h>
Void swap (int a [], int B [], int I, int size)
{
Int k, j, temp;
If (I = 3)
{
For (k = 0; k <3; k ++)
{
Printf ("% d", B [k]);
}
Printf ("\ n ");
Return;
}
Else
{
For (j = 0; j <size; j ++)
{
B [I] = a [j];
Temp = a [j]; a [j] = a [size-1]; a [size-1] = temp;
Swap (a, B, I + 1, size-1 );
Temp = a [j]; a [j] = a [size-1]; a [size-1] = temp;
}
}
}

Int main ()
{
Int a [5] = {1, 2, 3, 4, 5}, B [3]; // calculates the full arrangement of three numbers in five numbers.
Swap (a, B, 0, 5 );
Return 0;
}

Use n m numbers for combination:

[10 reverse replacement]

Algorithm idea:

(1) Initialize an array of m elements (all composed of 0, 1), and initialize the first n elements to 1, followed by 0. At this time, the first combination sequence can be output.
(2) Search for the first 10 combinations, convert them to 01, and then push all the 1 in front of the 10 combinations to the left, that is, ensure that the first 1 is at the leftmost. Then, a group of merging sequences can be output.
(3) Repeat Step (2) and find that no 10 combination positions are found. All possibilities have been output.

Copy codeThe Code is as follows: # include <stdio. h>
# Include <stdlib. h>
Void putout (int * num, int m)
{
Int I;
For (I = 0; I <m; I ++)
{
If (* (num + I ))
Printf ("% d", I + 1 );

}
Printf ("\ n ");
}

Int check (int * num, int m, int n)
{
Int flag = 1, I; // when flag = 1, continue the while loop. Otherwise, exit the loop.
For (I = 0; I <m-n; I ++)
{
If (* (num + I ))
{
Return 1;
}
}
Return 0;
}

Void choseNum (int * num, int m, int n)
{
Int I, j;
Putout (num, m); // output the first combination
While (1)
{
Int count = 0; // note that it has been debugged for a long time at the count position.
// Find the first 1 0 combination
For (I = 0; I <m-1; I ++)
{
If (* (num + I) = 1 & * (num + I + 1) = 0)
{
* (Num + I) = 0;
* (Num + I + 1) = 1;
Break;
}
If (* (num + I) // counts the number of times 1 appears before
Count ++;
}
For (j = 0; j <I; j ++)
{
If (j <count) // set the preceding number to 1
{
* (Num + j) = 1;
}
Else // The last few digits are 0.
{
* (Num + j) = 0;
}
}
Putout (num, m );
If (check (num, m, n )! = 1)
Break;
}
Free (num );
}

Int main ()
{
Int m, n; // find n combinations from the number of m
Printf ("a combination of n numbers from m :");
Scanf ("% d", & m, & n );
Int * num, I;
// Int count;
Num = (int *) malloc (sizeof (int) * m );
For (I = 0; I <m; I ++)
{
If (I <n)
* (Num + I) = 1;
Else
* (Num + I) = 0;
}
ChoseNum (num, m, n );
Return 0;
}

Result example:

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.