【贪心】洛谷 P1107 最大整数

xiaoxiao2021-02-28  90

题目描述

设有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

代码

#include<iostream> #include<cstring> #include<algorithm> using namespace std; int comp(const string&a,const string&b) { return (a+b>b+a); } int main() { string number[25]; int n; while(scanf("%d",&n)==1) { for(int i=0;i<n;i++)cin>>number[i]; sort(number+0,number+n,comp); for(int i=0;i<n;i++)cout<<number[i]; } return 0; }
转载请注明原文地址: https://www.6miu.com/read-46328.html

最新回复(0)