輸入四個字母實現全排列

來源:互聯網
上載者:User

// 輸入四個字母實現全排列

#include <stdio.h>
#define MAX 30
typedef struct{
        char a0,a1,a2,a3;
        }string;//結構體類型
int main()
{
    string str[MAX];
    char ch,a0,a1,a2,a3,a[4];
   int n=0,i=0,j,k,m,q,num=0,sign=0;
    printf("please input 4 chars :/n");
    while((ch=getchar())!='/n')
          a[i++]=ch;  //存放字元
    printf("The result is :/n"); 
     for(i=0;i<4;i++)
         for(j=0;j<4;j++)
             for(k=0;k<4;k++)
                 for(m=0;m<4;m++) 
                 {  
                      if(i!=j&&i!=k&&i!=m&&j!=k&&j!=m&&k!=m)  //保證不重複輸出
                          {
                             str[n].a0=a[i];
                             str[n].a1=a[j];
                             str[n].a2=a[k];
                             str[n].a3=a[m];  //輸出的組合儲存到str[N]中
                             n++;  //記數
                            for(q=0;q<n-1;q++)//注意是n-1  
                                  {
                                        if(str[q].a0==a[i]&&str[q].a1==a[j]&&str[q].a2==a[k]&&str[q].a3==a[m] )
                                        goto next;
                                  }
                            printf("%c%c%c%c  ",a[i],a[j],a[k],a[m]);
                               num++;  //如果與前面不重複則輸出
                               next:
                                    ;
                        }//if
             } //for 
                     printf("/nnum=%d ",num);       
                     ch=getch();
                     return 0;
}        
                           

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.