UVa 10905 Children's Game / 贪心

本文介绍了一个使用贪心算法解决的问题:给定一组数字,通过特定的排序方式找到能组成最大数值的数字序列组合。文章提供了完整的C++实现代码,并展示了如何定义比较函数来实现所需的特殊排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给你n个数字 求一个组合 使n个数字按顺序组成的数最大 贪心 排序输出即可

#include <stdio.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
string a[100];

bool cmp(string a,string b)
{
	string s1,s2;
	s1 = a+b;
	s2 = b+a;
	return s1 > s2;
}
int main()
{
	int i,n;
	while(scanf("%d",&n),n)
	{
		for(i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		sort(a,a+n,cmp);
		string s;
		for(i = 0;i < n; i++)
		{
			s += a[i];
			//cout << a[i] << endl;
		}
		cout << s << endl;
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值