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
通常我們想這種問題,一定是比完之後再從新比頭,所以才會有這種想法
贈送表格:讓您解釋給你朋友聽
+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+
/***********************************************************/
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- main()
- {
- int a,b,c,n,mm;
- while(scanf("%d",&n)==1)
- {
- char x[12];
- int math[101][11],number[101]={0};
- for(a=0;a<n;a++)
- {
- scanf("%s",x);
- int m=strlen(x);
- number[a]=m;
- for(b=0,c=0;b<11;b++,c++)
- {
- math[a][b]=x[c];
- if(c==m-1)
- c=-1;
- }
- }
- for(a=0;a<n;a++)
- for(b=0;b<n-1;b++)
- {
- int flag=0;
- for(c=0;c<11;c++)
- {
- if(math[b][c]>math[b+1][c])/*找到前面的比較大 不用動*/
- {flag=0;break;}
- if(math[b+1][c]>math[b][c])
- {flag=1;break;}
- }
- if(flag==1)
- {
- int temp,temp1[21];
- for(c=0;c<11;c++)
- {
- temp1[c]=math[b][c];
- math[b][c]=math[b+1][c];
- math[b+1][c]=temp1[c];
- }
- temp=number[b];
- number[b]=number[b+1];
- number[b+1]=temp;
- }
- }
- for(a=0;a<n;a++)
- for(b=0;b<number[a];b++)
- printf("%c",math[a][b]);
- printf("\n");
- }
- return 0;
- }