NC16783 拼数 经典贪心

拼数

链接:https://ac.nowcoder.com/acm/problem/16783

设有n个正整数(n ≤ 20),将它们联接成一排,组成一个最大的多位整数。
例如:n=3时,3个整数13,312,343联接成的最大整数为:34331213
又如:n=4时,4个整数7,13,4,246联接成的最大整数为:7424613

  • s t r A + s t r B > s t r B + s t r A strA+strB>strB+strA strA+strB>strB+strA排序,后打印

    struct Node {
    	string str;
    	bool operator < (const Node& no) const {
    		return str+no.str > no.str+str;
    	}
    } a[MAXN];
    
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif


#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define QAQ (0)

using namespace std;

#ifdef debug
#define show(x...) \ do { \ cout << "\033[31;1m " << #x << " -> "; \ err(x); \ } while (0)
void err() { cout << "\033[39;0m" << endl; }
#endif

template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

int n, m, Q, K;

struct Node {
	string str;
	bool operator < (const Node& no) const {
		return str+no.str > no.str+str;
	}
} a[MAXN];

int main() {
#ifdef debug
	freopen("test", "r", stdin);
	clock_t stime = clock();
#endif
	scanf("%d ", &n);
	for(int i=1; i<=n; i++)
		cin >> a[i].str;
	sort(a+1, a+1+n);
	for(int i=1; i<=n; i++)
		cout << a[i].str;

#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}


全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务