题目描述
设有n个正整数,将他们连接成一排,组成一个最大的多位整数。
如:n=3时,3个整数13,312,343,连成的最大整数为34331213。
如:n=4时,4个整数7,13,4,246连接成的最大整数为7424613。
如:n=3时,3个整数13,312,343,连成的最大整数为34331213。
如:n=4时,4个整数7,13,4,246连接成的最大整数为7424613。
输入描述:
有多组测试样例,每组测试样例包含两行,第一行为一个整数N(N<=100),第二行包含N个数(每个数不超过1000,空格分开)。
输出描述:
每组数据输出一个表示最大的整数。
示例1
输入
2 12 123 4 7 13 4 246
输出
12312 7424613
#include<cstdio> #include<string> #include<algorithm> #include<cstring> using namespace std; char s[100][4]; int compare(int x,int y){//比较连接的字符串 char t1[100],t2[100]; strcpy(t1,s[x]); strcat(t1,s[y]);//连接字符串 strcpy(t2,s[y]); strcat(t2,s[x]); if(strcmp(t1,t2)<0)//t1<t2 return 1; else return 0; } int main(){ int n; while(~scanf("%d",&n)){ for(int i=0;i<n;i++) scanf("%s",s[i]); for(int i=0;i<n;i++){//选择排序 for(int j=i+1;j<n;j++){ if(compare(i,j)){ char temp[100]; strcpy(temp,s[i]); strcpy(s[i],s[j]); strcpy(s[j],temp); } } } for(int i=0;i<n;i++) printf("%s",s[i]); printf("\n"); } return 0; }