1. 程式人生 > >P1107 最大整數

P1107 最大整數

har clas blog code include str 位數 com ostream

題目描述

設有n個正整數 (n<=20), 將它們連接成一排, 組成一個最大的多位整數.

例如: n=3時, 3個整數13, 312, 343連接成的最大整數為: 34331213

又如: n=4時, 4個整數7,13,4,246連接成的最大整數為: 7424613

輸入輸出格式

輸入格式:

n n個數

輸出格式:

連接成的多位數

輸入輸出樣例

輸入樣例#1:
3
13 312 343
4
7 13 4 246
輸出樣例#1:
34331213
7424613


stl大法好啊,,總感覺這題在哪兒做過
註意一下條件不要寫a>b不然
3
9 90 9這組數據過不了!!
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 const int MAXN=21;
 8 int read(int & n)
 9 {
10     char c=.;int x=0,flag=0;
11     while
(c<0||c>9) 12 { 13 c=getchar(); 14 if(c==-)flag=1; 15 } 16 while(c>=0&&c<=9) 17 { 18 x=x*10+(c-48); 19 c=getchar(); 20 } 21 if(flag==1)n=-x; 22 else n=x; 23 } 24 string s[21]; 25 int comp(const string
& a,const string & b) 26 { 27 return a+b>b+a; 28 } 29 int main() 30 { 31 int n; 32 while(cin>>n) 33 { 34 for(int i=1;i<=n;i++) 35 cin>>s[i]; 36 sort(s+1,s+n+1,comp); 37 for(int i=1;i<=n;i++) 38 cout<<s[i]; 39 } 40 41 return 0; 42 }


P1107 最大整數