【牛客网】数串

题目描述

设有n个正整数,将他们连接成一排,组成一个最大的多位整数。
如: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;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值