拼数(结构体)

题目名字

拼数(结构体)

[题目链接]https://www.luogu.com.cn/problem/P1012?contestId=146378(https://www.acwing.com/problem/content/description/94/)

题意

给定几个数字,把他们拼成一个最大的数

思路

  1. 设置一个结构体去拼每两个数
  2. 比较每两个数拼起来是最大的
  3. 最后用sort把数组里每个数字排序再输出

算法一:

实现步骤
  1. 定义一个结构体数组
  2. 再比较
  3. 最后sort输出
代码
 
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int x=1e6+10;
struct num{
	string s;
}p[x];
bool cmp( num a,num b){
	return a.s+b.s>b.s+a.s;
}
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>p[i].s;
	}
	sort(p,p+n,cmp);
	for(int i=0;i<n;i++){
		cout<<p[i].s;
	}
	return 0;
}
  • 21
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值