YTUOJ-C語言實驗-排列

來源:互聯網
上載者:User

標籤:c   編程   printf   部落格   c語言   

Description有4個互不相同的數字,輸出由其中三個不重複數字組成的排列。Input4個整數。Output所有排列。Sample Input
1 2 3 4
Sample Output
1 2 31 3 22 1 32 3 13 1 23 2 11 2 41 4 22 1 42 4 14 1 24 2 11 3 41 4 33 1 43 4 14 1 34 3 12 3 42 4 33 2 43 4 24 2 34 3 2
HINT

Source

ZJGSU

 

 

代碼如下:

#include <stdio.h>int i,j,k,a[3],b[3],c[3];void Sort(int e[]){    for (i=0; i<3; i++)        a[i]=b[i]=c[i]=e[i];    for(i=0; i<3; i++)    {        for(j=0; j<3; j++)        {            for(k=0; k<3; k++)            {                if(a[i]==b[j]||a[i]==c[k]||b[j]==c[k])                    continue;                else                    printf("%d% d %d\n",a[i],b[j],c[k]);            }        }    }}int main(){    int i, d[4],e[3];    for (i=0; i<4; i++)        scanf("%d", d+i);    e[0]=d[0],e[1]=d[1],e[2]=d[2];    Sort(e);    e[0]=d[0],e[1]=d[1],e[2]=d[3];    Sort(e);    e[0]=d[0],e[1]=d[2],e[2]=d[3];    Sort(e);    e[0]=d[1],e[1]=d[2],e[2]=d[3];    Sort(e);    return 0;}


運行結果:

 

學習心得:

最近在嘗試著用C語言去做題,但是還是擺脫不了濃濃的C++氣息,經常因為帶上C++中的一些關鍵字而錯誤。

這道題其實一開始是沒有思路的,第一反應是枚舉,結果那隻適用於1,2,3,4,的情況。然後想到了用多個數組來儲存,能夠完美執行,但是還是錯了,它居然要求了排列順序,

於是進一步複雜化,一次分成了四次。總算還是AC了

YTUOJ-C語言實驗-排列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.