2009-02-14 13:11:32來源不明

96高市資訊學科能力競賽 第二題:排列最大值

這題看似簡單,要寫成程式卻一點頭緒都沒有?!
處理方法:(輸入的數字不會超過10位)
假使我輸入89 989 98 889 998 898
首先把不夠的補,重複儲存!
+-+-+-+-+-+-+-+-+-+-+
|8|9|8|9|8|9|8|9|8|9| ←89
+-+-+-+-+-+-+-+-+-+-+

+-+-+-+-+-+-+-+-+-+-+
|9|8|9|9|8|9|8|9|8|9| ←989
+-+-+-+-+-+-+-+-+-+-+
+-+-+-+-+-+-+-+-+-+-+
|9|8|9|8|9|8|9|8|9|8| ←98
+-+-+-+-+-+-+-+-+-+-+
+-+-+-+-+-+-+-+-+-+-+
|8|8|9|8|8|9|8|8|9|8| ←889
+-+-+-+-+-+-+-+-+-+-+
+-+-+-+-+-+-+-+-+-+-+
|9|9|8|9|9|8|9|9|8|9| ←998
+-+-+-+-+-+-+-+-+-+-+
+-+-+-+-+-+-+-+-+-+-+
|8|9|8|8|9|8|8|9|8|8| ←898
+-+-+-+-+-+-+-+-+-+-+
之後把表格當作是一個數字,利用大數的比大小
出來的就是...998 989 98 89 898 889 
9989899889898889
通常我們想這種問題,一定是比完之後再從新比頭,所以才會有這種想法
贈送表格:讓您解釋給你朋友聽
+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+

/***********************************************************/

  1. #include<stdio.h>   
  2. #include<stdlib.h>   
  3. #include<string.h>   
  4. main()   
  5. {   
  6.  int a,b,c,n,mm;   
  7.  while(scanf("%d",&n)==1)   
  8.   {   
  9.    char x[12];   
  10.    int math[101][11],number[101]={0};   
  11.    for(a=0;a<n;a++)   
  12.     {   
  13.      scanf("%s",x);   
  14.      int m=strlen(x);   
  15.      number[a]=m;   
  16.      for(b=0,c=0;b<11;b++,c++)   
  17.       {   
  18.        math[a][b]=x[c];   
  19.        if(c==m-1)   
  20.         c=-1;   
  21.       }   
  22.     }   
  23.    for(a=0;a<n;a++)   
  24.     for(b=0;b<n-1;b++)   
  25.      {   
  26.       int flag=0;   
  27.       for(c=0;c<11;c++)   
  28.        {   
  29.         if(math[b][c]>math[b+1][c])/*找到前面的比較大 不用動*/  
  30.          {flag=0;break;}   
  31.         if(math[b+1][c]>math[b][c])   
  32.          {flag=1;break;}    
  33.        }   
  34.       if(flag==1)   
  35.        {   
  36.         int temp,temp1[21];   
  37.         for(c=0;c<11;c++)   
  38.          {   
  39.           temp1[c]=math[b][c];   
  40.           math[b][c]=math[b+1][c];   
  41.           math[b+1][c]=temp1[c];   
  42.          }   
  43.          temp=number[b];   
  44.          number[b]=number[b+1];   
  45.          number[b+1]=temp;   
  46.        }    
  47.      }    
  48.      for(a=0;a<n;a++)   
  49.       for(b=0;b<number[a];b++)   
  50.        printf("%c",math[a][b]);   
  51.        printf("\n");   
  52.   }   
  53.  return 0;   
  54. }