"Full arrangement" Hdu1027ignatius and the Princess ii/hdu1716 arrangement 2

Source: Internet
Author: User

Full array function, 666, the last Blue Bridge Cup was stained with the whole line of light ....

Next_pernutation ();

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1716

Problem Descriptionray also has an interest in the column of numbers:
The existing four cards, with these four cards can be arranged in a number of different 4-digit numbers, which require the output of these 4 digits in order from small to large.

Input has one row for each set of data, representing the number on the four cards (0<= digit <=9), and if four cards are 0, enter the end.

Output outputs each group of cards in a small-to-large order of 4 digits that can consist of these four cards, with the same number of thousands in the same row, separated by a space between each four-digit number in the same row.
There is an empty row between each set of output data, and there is no blank line after the last set of data.

Sample Input
1 2 3 41 1 2 30 1 2 30 0 0 0

Sample Output
1234 1243 1324 1342 1423 14322134 2143 2314 2341 2413 24313124 3142 3214 3241 3412 34214123 4132 4213 4231 4312 43211123 1 132 1213 1231 1312 13212113 2131 23113112 3121 32111023 1032 1203 1230 1302 13202013 2031 2103 2130 2301 23103012 3021 310 2 3120 3201 3210

Note the format ...

AC Code:

#include <iostream> #include <algorithm> #include <cstring> #include <cstdio>using namespace    Std;int Main () {int a[5],temp=0;      Freopen ("In.txt", "R", stdin);    Debug also to delete//freopen ("OUT.txt", "w", stdout); while (Cin>>a[0]>>a[1]>>a[2]>>a[3]) {if (!a[0]&&!a[1]&&!a[2]&&!a[        3]) {break;        } if (temp) cout<<endl;        Format is important ...        Temp=1;        Sort (a,a+4);        int i=0;        while (a[i]==0) {i++;        } if (a[0]==0) swap (a[i],a[0]);        int ans=a[0],flag=0;                do{if (ans!=a[0]) {ans=a[0];                flag=0;            cout<<endl;            }else if (flag) cout<< ';            cout<<a[0]<<a[1]<<a[2]<<a[3];        flag=1;        }while (Next_permutation (a,a+4));    cout<<endl; } return 0;}

Topic Links: http://acm.hdu.edu.cn/showproblem.php?pid=1027

Problem Descriptionnow Our hero finds the door to the Beelzebub feng5166. He opens the door and finds feng5166 is on to kill our pretty princess. But now the Beelzebub have to beat our hero first. feng5166 says, "I had three question for you, if you can work them out, I'll release the princess, or you'll be my di Nner, too. " Ignatius says confidently, "OK, at last, I'll save the princess."

"Now I'll show you the first problem." feng5166 says, "Given a sequence of number 1 to N, we define that 1,2,3...n-1,n I s the smallest sequence among all the sequence which can is composed with number 1 to N (each number can is and should be u Se only once in this problem). So it's easy to see the second smallest sequence is 1,2,3...n,n-1. Now I'll give you and numbers, N and M. You should tell me the Mth smallest sequence which are composed with number 1 to N. It's easy, isn ' t? Hahahahaha ... "
Can Ignatius to solve this problem?

Inputthe input contains several test cases. Each test case consists of numbers, N and M (1<=n<=1000, 1<=m<=10000). You may assume this there is always a sequence satisfied the Beelzebub ' s demand. The input is terminated by the end of file.

Outputfor each of the test case, you are only having to output the sequence satisfied the Beelzebub ' s demand. When output A is sequence, you should print a space between the other numbers, but does not output any spaces after the last number.

Sample Input
6 411 8

Sample Output
1 2 3 5 6 41 2 3 4 5 6 7 9 8 11 10

STL Dafa next_pernutation () AC code:

#include <iostream> #include <algorithm> #include <cstring> #include <cstdio>using namespace Std;int Main () {    int n,m;    Cin.sync_with_stdio (false);    while (cin>>n>>m) {        int a[1005];        for (int i=1;i<=n;i++)            a[i]=i;        for (int i=1;i<m;i++)            next_permutation (a+1,a+n+1);        for (int i=1;i<=n;i++) {            cout<<a[i];            if (i<n) cout<< ";            else cout<<endl;        }    }    return 0;}

Comtop Inverse Expansion AC code:

#include <iostream>using namespace Std;int fac[]={1,1,2,6,24,120,720,5040,40320};        N!int ans[10001];      An array of stored answers; int len=0;    Record the length of the array,//n for a total of several numbers, K for the number of digits, num for the n number of Cantor (int n,int k,int num) {k--;    int i,j,vis[11]={0};     for (i=0;i<n;i++) {int t=k/fac[n-i-1]; (Number of the following!)                A multiple of t=0, which indicates the minimum number of positions, for (j=1;j<=n;j++) {if (!vis[j]) {//Find no small number of T-occurrences;                if (t==0) break;            t--;        }} vis[j]=1;        K%=FAC[N-I-1];    Ans[len++]=j+num-1;    }}int Main () {int n,m;    Cin.sync_with_stdio (FALSE);        while (cin>>n>>m) {len=0;        int i=1,j;            if (n>8) {int t1=n%8;            int t2= (n/8-1) *8;            for (i=1;i<=t1;i++)//8 A multiple of the extra is definitely not moving ans[len++]=i;            A multiple of for (j=0;j<t2;j++,i++)//8 Removes the last 8-bit ans[len++]=i;        Cantor (8,m,i);        }else Cantor (n,m,i); for (int i=0; i<n;i++) {if (i<n-1) cout<<ans[i]<< ";        else cout<<ans[i]<<endl; }} return 0;}


"Full arrangement" Hdu1027ignatius and the Princess ii/hdu1716 arrangement 2

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.