天天看點

菜雞成長之路之STL

菜雞成長之路之STL

A - 圓桌問題

圓桌上圍坐着2n個人。其中n個人是好人,另外n個人是壞人。如果從第一個人開始數數,數到第m個人,則立即處死該人;然後從被處死的人之後開始數數,再将數到的第m個人處死……依此方法不斷處死圍坐在圓桌上的人。試問預先應如何安排這些好人與壞人的座位,能使得在處死n個人之後,圓桌上圍坐的剩餘的n個人全是好人。

Input

多組資料,每組資料輸入:好人和壞人的人數n(<=32767)、步長m(<=32767);

Output

對于每一組資料,輸出2n個大寫字母,‘G’表示好人,‘B’表示壞人,50個字母為一行,不允許出現空白字元。相鄰資料間留有一空行。

Sample Input

2 3

2 4

Sample Output

GBBG

BGGB

題意:約瑟夫環問題,可以用連結清單也可以利用vector模拟處死的過程,最終剩餘在vector中的都是好人(此處是vector)

AC代碼:

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<string.h>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 50005;
const double PI = acos(-1.0);
#define reset(x) memset(x,0,sizeof(x))
typedef long long int ll;  
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); 
    int n,m;
    while(cin>>n>>m){
        vector<int> tab;
        for(int i=0;i<2*n;i++)     //初始化所有人
        tab.push_back(i);
        int pf=0;                   //從1号開始數
        for(int i=0;i<n;i++)       //總共n個壞人
        {
            pf=(pf+m-1)%tab.size();
            tab.erase(tab.begin()+pf);
        }
        int j=0;
        for(int i=0;i<2*n;i++){
            if(j<n&&i==tab[j])
            {
                j++;
                cout<<'G';
            }
            else
            cout<<'B';
            if((i+1)%50==0) cout<<endl;
        }
        cout<<endl<<endl;
    }
    return 0;
}
           

B - Text Reverse

Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.

Each test case contains a single line with several words. There will be at most 1000 characters in a line.

Output

For each test case, you should output the text which is processed.

Sample Input

3

olleh !dlrow

m’I morf .udh

I ekil .mca

Sample Output

hello world!

I’m from hdu.

I like acm.

Hint

Remember to use getchar() to read ‘\n’ after the interger T, then you may use gets() to read a line and process it.

題意:讀入一行帶空格的字元串,将每個單詞逆序輸出(單詞之間用空格隔開),提示:用getchar()将輸入第一個整數後的換行符吞掉。

本題每行輸入都以回車結尾,将輸入處理好可以使用gets()或是getline讀入整行字元串,利用一個二維字元數組将單詞逆序輸出

AC代碼:

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<string.h>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 50005;
const double PI = acos(-1.0);
#define reset(x) memset(x,0,sizeof(x))
typedef long long int ll;  
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); 
    int n;
    char c;
//    cin>>n;
	scanf("%d",&n);
    getchar();
    while(n--){
        vector<char>str[1001];
        int i=0;
        while (1)
        {
            c=getchar();
            if(c=='\n'||c==EOF)
            break;
            if(c!=' ')
            str[i].push_back(c);
            else
            i++;
        }
        i++;
        for(int j=0;j<i;j++)
        {
            for(int k=str[j].size()-1;k>=0;k--)
            cout<<str[j][k];
            //輸出一個單詞輸出一個空格
            if(j!=i-1)
            cout<<' ';
        }
        cout<<endl;
    }
    return 0;
}
           

C - ACboy needs your help again!

ACboy was kidnapped!!

he miss his mother very much and is very scare now.You can’t image how dark the room he was put into is, so poor 😦.

As a smart ACMer, you want to get ACboy out of the monster’s labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can’t solve my problems, you will die with ACboy."

The problems of the monster is shown on the wall:

Each problem’s first line is a integer N(the number of commands), and a word “FIFO” or “FILO”.(you are very happy because you know “FIFO” stands for “First In First Out”, and “FILO” means “First In Last Out”).

and the following N lines, each line is “IN M” or “OUT”, (M represent a integer).

and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!

Input

The input contains multiple test cases.

The first line has one integer,represent the number oftest cases.

And the input of each subproblem are described above.

Output

For each command “OUT”, you should output a integer depend on the word is “FIFO” or “FILO”, or a word “None” if you don’t have any integer.

Sample Input

4

4 FIFO

IN 1

IN 2

OUT

OUT

4 FILO

IN 1

IN 2

OUT

OUT

5 FIFO

IN 1

IN 2

OUT

OUT

OUT

5 FILO

IN 1

IN 2

OUT

IN 3

OUT

Sample Output

1

2

2

1

1

2

None

2

3

題意:很直接的一個題,輸入為FIFO用隊列模拟,FILO用棧模拟即可。

AC代碼:

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 50005;
const double PI = acos(-1.0);
#define reset(x) memset(x,0,sizeof(x))
typedef long long int ll;  
int stack_sol(int n){
    stack<int> a; 
    for(int i=0;i<n;i++)
    {
        string str;
        int x;
        cin>>str;
        if(str=="IN")
        {
            cin>>x;
            a.push(x);
        }
        else
        {
            if(!a.empty()){
                cout<<a.top()<<endl;
                a.pop();
            }
            else
            {
                cout<<"None"<<endl;
            }
            
        }
    }
    return 0;
}
int queue_sol(int n){
    queue<int> a; 
    for(int i=0;i<n;i++)
    {
        string str;
        int x;
        cin>>str;
        if(str=="IN")
        {
            cin>>x;
            a.push(x);
        }
        else
        {
            if(!a.empty()){
                cout<<a.front()<<endl;
                a.pop();
            }
            else
            {
                cout<<"None"<<endl;
            }
            
        }
    }
    return 0;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); 
    int t;
    cin>>t;
    while(t--){
        int n;
        string s;
        cin>>n>>s;
        if(s=="FIFO")
        queue_sol(n);
        else
        stack_sol(n);
    }
    return 0;
}
           

D - 簡單電腦

讀入一個隻包含 +, -, *, / 的非負整數計算表達式,計算該表達式的值。

Input

測試輸入包含若幹測試用例,每個測試用例占一行,每行不超過200個字元,整數和運算符之間用一個空格分隔。沒有非法表達式。當一行中隻有0時輸入結束,相應的結果不要輸出。

Output

對每個測試用例輸出1行,即該表達式的值,精确到小數點後2位。

Sample Input

1 + 2

4 + 2 * 5 - 7 / 11

Sample Output

3.00

13.36

題意:将中綴表達式轉字尾表達式,再利用字尾表達式計算算式結果(關于中綴表達式轉字尾表達式可參考另一篇部落格:https://blog.csdn.net/newbie_dqt/article/details/113784534)

AC代碼:

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 50005;
const double PI = acos(-1.0);
#define reset(x) memset(x,0,sizeof(x))
typedef long long int ll;  
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int tab[50];
    double strtab[50];
    //double、double、double!!!找半天錯,結果是精度有問題
    strtab['+']=-1;strtab['-']=-2;strtab['*']=-3;strtab['/']=-4;//約定-1表示+,-2表示-,-3表示*,-4表示/
    tab['+']=tab['-']=1;    //優先級表
    tab['*']=tab['/']=2;
    while(1){
        stack<char>tmp;
        vector<double>str;//存放字尾表達式,約定-1表示+,-2表示-,-3表示*,-4表示/
        string s;
        getline(cin,s);
        if(s=="0") break;
        for(int i=0;i<s.size();)
        {
            if(s[i]==' ')
            {
                i++;
                continue;
            }
            else if(s[i]>='0'&&s[i]<='9'){  //數字
                double intg=0;
                while (s[i]>='0'&&s[i]<='9')
                {
                    intg=intg*10+s[i]-'0';
                    i++;
                }
                str.push_back(intg);    //數字直接存放
            }
            else//運算符(若題目中有括号的輸入,則左括号優先級最高無條件入棧,在遇到右括号後
            {   //将棧頂符号彈出儲存直至遇到左括号,注意左右括号并不在字尾表達式中)
                if(tmp.empty()||tab[tmp.top()]<tab[s[i]])   //棧為空或者待處理字元優先級高于棧頂字元
                tmp.push(s[i]);
                else        //否則一直出棧直至棧為空或者待處理字元優先級高于棧頂元素優先級,再将待處理字元入棧
                {
                    while ((!tmp.empty())&&tab[s[i]]<=tab[tmp.top()])//出棧直至棧頂運算符優先級低于待處理運算符s[i]
                    {
                        str.push_back(strtab[tmp.top()]);
                        tmp.pop();
                    }
                    tmp.push(s[i]);
                }
                i++;
            }
        }
        
        //周遊結束後将運算符中轉棧tmp中剩餘運算符存入字尾表達式中
        while (!tmp.empty())
        {
            str.push_back(strtab[tmp.top()]);
            tmp.pop();
        }
        //利用字尾表達式開始運算
        stack<double>res;
        for(int i=0;i<str.size();i++){
            if(str[i]>=0) res.push(str[i]);
            else    //周遊到運算符:約定-1表示+,-2表示-,-3表示*,-4表示/
            {
                double b=res.top();
                res.pop();
                double a=res.top();
                res.pop();
                double c;
                if(str[i]==-1) c=a+b;
                if(str[i]==-2) c=a-b;
                if(str[i]==-3) c=a*b;
                if(str[i]==-4) c=a/b;
                res.push(c);
            }
        }
        printf("%.2f\n",res.top());
        res.pop();
    }
    return 0;
}
           

E - 看病要排隊

看病要排隊這個是地球人都知道的常識。

不過經過細心的0068的觀察,他發現了醫院裡排隊還是有講究的。0068所去的醫院有三個醫生(汗,這麼少)同時看病。而看病的人病情有輕重,是以不能根據簡單的先來先服務的原則。是以醫院對每種病情規定了10種不同的優先級。級别為10的優先權最高,級别為1的優先權最低。醫生在看病時,則會在他的隊伍裡面選擇一個優先權最高的人進行診治。如果遇到兩個優先權一樣的病人的話,則選擇最早來排隊的病人。

現在就請你幫助醫院模拟這個看病過程。

Input

輸入資料包含多組測試,請處理到檔案結束。

每組資料第一行有一個正整數N(0<N<2000)表示發生事件的數目。

接下來有N行分别表示發生的事件。

一共有兩種事件:

1:“IN A B”,表示有一個擁有優先級B的病人要求醫生A診治。(0<A<=3,0<B<=10)

2:“OUT A”,表示醫生A進行了一次診治,診治完畢後,病人出院。(0<A<=3)

Output

對于每個"OUT A"事件,請在一行裡面輸出被診治人的編号ID。如果該事件時無病人需要診治,則輸出"EMPTY"。

診治人的編号ID的定義為:在一組測試中,"IN A B"事件發生第K次時,進來的病人ID即為K。從1開始編号。

Sample Input

7

IN 1 1

IN 1 2

OUT 1

OUT 2

IN 2 1

OUT 2

OUT 1

2

IN 1 1

OUT 1

Sample Output

2

EMPTY

3

1

1

題意:根據題目描述,用結構體存儲病人序号以及優先級,選用優先隊列,重寫仿函數或者重載操作符(參考部落格:https://www.cnblogs.com/Deribs4/p/5657746.html)

AC代碼:

//如果隊列數比較多可以使用數組,我比較懶直接開三個優先隊列,然後代碼直接寫一份copy兩份,代碼比較長,不太美觀

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 50005;
const double PI = acos(-1.0);
#define reset(x) memset(x,0,sizeof(x))
typedef long long int ll;  
struct vj
{
    int index;
    int pri;
    // friend bool operator<(vj a0,vj a1){		//重載<
    //     if(a0.pri!=a1.pri) return a0.pri<a1.pri;
    //     else return a0.index>a1.pri;
    // }
};
struct cmp
{
    bool operator()(vj a0,vj a1){
        if(a0.pri==a1.pri) return a0.index>a1.index;
        else
        return a0.pri<a1.pri;
    }
};
int doc1(priority_queue<vj,vector<vj>,cmp>&d1,string s,int b,int i){
    if(s=="IN"){
        vj t;
        t.index=i;
        t.pri=b;
        d1.push(t);
    }
    else
    {
        if(!d1.empty()){
            cout<<d1.top().index<<endl;
            d1.pop();
        }
        else
        {
            cout<<"EMPTY"<<endl;
        }
    }
    
    return 0;
}
int doc2(priority_queue<vj,vector<vj>,cmp>&d2,string s,int b,int i){
    if(s=="IN"){
        vj t;
        t.index=i;
        t.pri=b;
        d2.push(t);
    }
    else
    {
        if(!d2.empty()){
            cout<<d2.top().index<<endl;
            d2.pop();
        }
        else
        {
            cout<<"EMPTY"<<endl;
        }
    }
    return 0;
}
int doc3(priority_queue<vj,vector<vj>,cmp>&d3,string s,int b,int i){
    if(s=="IN"){
        vj t;
        t.index=i;
        t.pri=b;
        d3.push(t);
    }
    else
    {
        if(!d3.empty()){
            cout<<d3.top().index<<endl;
            d3.pop();
        }
        else
        {
            cout<<"EMPTY"<<endl;
        }
    }
    return 0;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    while (cin>>n)
    {
        priority_queue<vj,vector<vj>,cmp>d1;
        priority_queue<vj,vector<vj>,cmp>d2;
        priority_queue<vj,vector<vj>,cmp>d3;
        int i=0;
        int l=1;
        while (n--)
        {
            int a,b;
            string s;
            cin>>s;
            if(s=="IN"){
                cin>>a>>b;
                i++;
            }
            else
            {
                cin>>a;
            }
            
            if(a==1)
            doc1(d1,s,b,i);
            else if(a==2)
            doc2(d2,s,b,i);
            else
            doc3(d3,s,b,i);
        }
        
    }
    return 0;
}
           

F - 士兵隊列訓練問題

某部隊進行新兵隊列訓練,将新兵從一開始按順序依次編号,并排成一行橫隊,訓練的規則如下:從頭開始一至二報數,凡報到二的出列,剩下的向小序号方向靠攏,再從頭開始進行一至三報數,凡報到三的出列,剩下的向小序号方向靠攏,繼續從頭開始進行一至二報數。。。,以後從頭開始輪流進行一至二報數、一至三報數直到剩下的人數不超過三人為止。

Input

本題有多個測試資料組,第一行為組數N,接着為N行新兵人數,新兵人數不超過5000。

Output

共有N行,分别對應輸入的新兵人數,每行輸出剩下的新兵最初的編号,編号之間有一個空格。

Sample Input

2

20

40

Sample Output

1 7 19

1 19 37

題意:見題述…

可以用vector,也可以用list,但是資料較大時用vector删除操作會有将數組剩餘的數前移的操作,時間消耗大。用list(連結清單)插入删除操作簡單,但是不能下标随機通路元素,通常用疊代器通路

根據題目要求選用list,模拟删除(出列)的過程。

AC代碼:

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
using namespace std;
const int N = 50005;
const double PI = acos(-1.0);
#define P pair<int,int>
#define reset(x) memset(x,0,sizeof(x))
typedef long long int ll;  
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        list<int> q;
        // vector<int>q;
        for(int i=0;i<n;i++)
            q.push_back(i+1);
        int key=2;
        while(q.size()>3){
            int count=1;
            for(list<int>::iterator pos=q.begin();pos!=q.end();){
                if(count%key==0)
                pos=q.erase(pos);   //指向删除後的下一進制素
                else
                pos++;
                count++;            //往後報數
            }
            if(key==2) key=3;       //1-2、1-3交替
            else key=2;
        }
        for(auto i=q.begin();i!=q.end();i++)
        // if(i==q.end())
        if(i==q.begin()) cout<<*i;
        else
        cout<<' '<<*i;
        cout<<endl;
    }
    
    return 0;
}
           

G - 産生冠軍

有一群人,打乒乓球比賽,兩兩捉對撕殺,每兩個人之間最多打一場比賽。

球賽的規則如下:

如果A打敗了B,B又打敗了C,而A與C之間沒有進行過比賽,那麼就認定,A一定能打敗C。

如果A打敗了B,B又打敗了C,而且,C又打敗了A,那麼A、B、C三者都不可能成為冠軍。

根據這個規則,無需循環較量,或許就能确定冠軍。你的任務就是面對一群比賽選手,在經過了若幹場撕殺之後,确定是否已經實際上産生了冠軍。

Input

輸入含有一些選手群,每群選手都以一個整數n(n<1000)開頭,後跟n對選手的比賽結果,比賽結果以一對選手名字(中間隔一空格)表示,前者戰勝後者。如果n為0,則表示輸入結束。

Output

對于每個選手群,若你判斷出産生了冠軍,則在一行中輸出“Yes”,否則在一行中輸出“No”。

Sample Input

3

Alice Bob

Smith John

Alice Smith

5

a c

c d

d e

b e

a d

Sample Output

Yes

No

題意:見題述…

可以将每一輪的勝利者放入集合win,将失敗者放入lose中,最後将win和lose中都出現過的名字從win中剔除(即win-lose),得到的新集合c即為最終勝利者名單,判斷c的基數為1則可以産生冠軍,否則不能

AC代碼:

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
using namespace std;
const int N = 50005;
const double PI = acos(-1.0);
#define P pair<int,int>
#define reset(x) memset(x,0,sizeof(x))
typedef long long int ll;  
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    while(cin>>n&&n)
    {
        set<string> win;
        set<string> lose;
        while (n--)
        {
            string a,b;
            cin>>a>>b;
            win.insert(a);
            lose.insert(b);
        }
        set<string>c;
        set_difference(win.begin(),win.end(),lose.begin(),lose.end(),insert_iterator<set<string>>(c,c.begin()));
        //用set_difference方法可以直接求出勝利者,但題目中并未要求求出勝利者的名字,是以也可以不必求c集合,改在在集合win中存放
        //所有參賽者的名字,在lose中存放失敗者的名字,直接用集合win的基數減去集合lose的基數,若結果為1則輸出Yes,否則輸出No
        if(c.size()==1) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}
           

H - Shopping

Every girl likes shopping,so does dandelion.Now she finds the shop is increasing the price every day because the Spring Festival is coming .She is fond of a shop which is called “memory”. Now she wants to know the rank of this shop’s price after the change of everyday.

Input

One line contians a number n ( n<=10000),stands for the number of shops.

Then n lines ,each line contains a string (the length is short than 31 and only contains lowercase letters and capital letters.)stands for the name of the shop.

Then a line contians a number m (1<=m<=50),stands for the days .

Then m parts , every parts contians n lines , each line contians a number s and a string p ,stands for this day ,the shop p 's price has increased s.

Output

Contains m lines ,In the ith line print a number of the shop “memory” ‘s rank after the ith day. We define the rank as :If there are t shops’ price is higher than the “memory” , than its rank is t+1.

Sample Input

3

memory

kfc

wind

2

49 memory

49 kfc

48 wind

80 kfc

85 wind

83 memory

Sample Output

1

2

題意:輸入n家商店、m天,每天都有n行輸入,每行一個數字a,一個字元串b,數字a代表b商店在本天漲價a元,總共輸入m次,要求輸出每天漲價過後memory商店的漲價排名

AC代碼:

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
using namespace std;
const int N = 50005;
const double PI = acos(-1.0);
#define P pair<int,int>
#define reset(x) memset(x,0,sizeof(x))
typedef long long int ll;  
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    while (cin>>n)
    {
        string s;
        for(int i=0;i<n;i++)
            cin>>s;
        int m;
        cin>>m;
        //map<int,string>a;       //不能用漲價當下标,不同商店漲價有可能相同
        map<string,int> a;
        while (m--)
        {
            int pr;
            for(int i=0;i<n;i++)
            {
                cin>>pr>>s;
                a[s]+=pr;
            }
            int rank=1;
            for(auto it=a.begin();it!=a.end();it++){
                if(it->second>a["memory"]) rank++;
            }   
            cout<<rank<<endl;
        }
    }
    
    return 0;
}

***
I - Ignatius and the Princess II
Now our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has to beat our hero first. feng5166 says, "I have three question for you, if you can work them out, I will release the Princess, or you will be my dinner, too." Ignatius says confidently, "OK, at last, I will save the Princess."

"Now I will show you the first problem." feng5166 says, "Given a sequence of number 1 to N, we define that 1,2,3...N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once in this problem). So it's easy to see the second smallest sequence is 1,2,3...N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It's easy, isn't is? Hahahahaha......"
Can you help Ignatius to solve this problem?
Input
The input contains several test cases. Each test case consists of two numbers, N and M(1<=N<=1000, 1<=M<=10000). You may assume that there is always a sequence satisfied the BEelzebub's demand. The input is terminated by the end of file.
Output
For each test case, you only have to output the sequence satisfied the BEelzebub's demand. When output a sequence, you should print a space between two numbers, but do not output any spaces after the last number.
Sample Input
6 4
11 8
Sample Output
1 2 3 5 6 4
1 2 3 4 5 6 7 9 8 11 10
題意:給定n,輸出1~n所有排列按照字典序排列的第m小的排列,用algorithm裡的next_permutation函數即可實作.
```cpp
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
using namespace std;
const int N = 1005;
const double PI = acos(-1.0);
#define P pair<int,int>
#define reset(x) memset(x,0,sizeof(x))
typedef long long int ll;  
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m;
    while (cin>>n>>m)
    {
        // char s[N];
        int s[N],k=2;;
        for(int i=0;i<n;i++)
            s[i]=i+1;
        if(m==1) goto loop;
        while (next_permutation(s,s+n))
        {
            if(k==m) break;
            k++;
        }
    loop:    
        for(int i=0;i<n;i++)
        if(i<n-1)
        cout<<s[i]<<' ';
        else cout<<s[i];
        cout<<endl;
    }
    return 0;
}
           

J - 排列2

Ray又對數字的列産生了興趣:

現有四張卡片,用這四張卡片能排列出很多不同的4位數,要求按從小到大的順序輸出這些4位數。

Input

每組資料占一行,代表四張卡片上的數字(0<=數字<=9),如果四張卡片都是0,則輸入結束。

Output

對每組卡片按從小到大的順序輸出所有能由這四張卡片組成的4位數,千位數字相同的在同一行,同一行中每個四位數間用空格分隔。

每組輸出資料間空一行,最後一組資料後面沒有空行。

Sample Input

1 2 3 4

1 1 2 3

0 1 2 3

0 0 0 0

Sample Output

1234 1243 1324 1342 1423 1432

2134 2143 2314 2341 2413 2431

3124 3142 3214 3241 3412 3421

4123 4132 4213 4231 4312 4321

1123 1132 1213 1231 1312 1321

2113 2131 2311

3112 3121 3211

1023 1032 1203 1230 1302 1320

2013 2031 2103 2130 2301 2310

3012 3021 3102 3120 3201 3210

題意:見題述…本題和上題一樣用next_permutation産生所有排列,唯一差別就是輸出格式的控制

AC代碼:

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
using namespace std;
const int N = 1005;
const double PI = acos(-1.0);
#define P pair<int,int>
#define reset(x) memset(x,0,sizeof(x))
typedef long long int ll;  
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    char a,b,c,d;
    int f1=0;
    while (cin>>a>>b>>c>>d)
    {
    	string s;
        s+=a;s+=b;s+=c;s+=d;
        if(s=="0000"){
            break;
        }
        if(f1==0) f1=1;
        else cout<<endl;
        sort(s.begin(),s.end());
        int f=0;
        do
        {
            if(s[0]>'0'&&f==0){
                cout<<s;
                c=s[0];
                f=1;
                continue;
            }
            if(s[0]>'0')
            {
                if(s[0]==c)
                    cout<<' ';
                else
                {
                    cout<<endl;
                    c=s[0];
                }
                cout<<s;
            }
        }while(next_permutation(s.begin(),s.end()));
        cout<<endl;
    }
    return 0;
}
           

繼續閱讀