Arranging combinatorial problems with recursive writing

Source: Internet
Author: User

Recently recursive people headache, but these two days to see also a little summed up some of the skills can not be called skill it

The problem as follows, will 1,2,3,4 the output of these four digital permutations, see the online there is a very two methods, that is, the number of 10000 within the output of the filter again, a little bit of language, but the program is quite good, hey

Return to the subject, with the idea of recursion to solve

(1) The method of rotating numbers, when the step is 1 o'clock, 1234 or 1234, the step is 2, 1234 can become 1243, 1324 ..., the step is 3, 1234 can be 1423, and so on, The most important thing that can be recursive is that you can rotate the number of each recursive one at a time.

#include <stdio.h>
void output (int *p,int count)
{
for (int j=0;j<count;j++)
{
printf ("%d", p[j]);
}
printf ("\ n");
}
void Differentsort (int *p,int j,int count)
{
if (j<count-1)
{
for (int i=j;i<count;i++)
{
int temp=p[i];
for (int k=i;k>=j;k--)
{
P[K]=P[K-1];
}
P[j]=temp;
Differentsort (P,j+1,count);
int TEMP1=P[J];
for (int k1=j;k1<i;k1++)
{
P[K1]=P[K1+1];
}
P[I]=TEMP1;
}
}
Else
{
Output (P,count);
}
}
void Main ()
{
int *p=new int;
P[0]=1;
p[1]=2;
p[2]=3;
p[3]=4;
Differentsort (p,0,4);

}

The most important function of nature is the Differentsort () function.

(1) The variable j is processed from the first, the variable I controls the size of the processing area, I-j+1 is the number of numbers to be processed, and K is the control variable that rotates the number of i-j+1. J starts with the No. 0 number, and I slowly gets larger from J to the array length.

(2) Recursive when the last one, that is, the case of j=3, the direct output 1234, and then consider the situation of j=2, output 1243, and p to 1234, and then consider the situation of j=1 p to 1324, and then to this p execution Differentsort (p,2,count) To 1342, then revert to 1324 and then revert to 1234;

In the same j=0 case, when I=1, p changes to 2134 and then to 2134 execution Differentsort (p,2,count) first becomes 2143 ... In short, first execute the last function, and output ... This is going to have to be realized.

Arranging combinatorial problems with recursive writing

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.