洛谷 P1781 宇宙总统

该博客介绍了如何解决洛谷P1781题目的挑战,即在数据超出long long范围的情况下,通过自定义排序方法cmp对超长整数进行排序,以找到最大数及其编号。博主提供了使用C++实现的字符串存储和排序的代码片段。
摘要由CSDN通过智能技术生成

题目大意

给出m个数,第i个数的编号为i,试求最大的数的编号及其数值(以字符串形式输出即可)

 

自定义排序方法cmp

bool cmp(person x, person y)
{
	if (x.score.size() == y.score.size())
		return x.score > y.score;
	else
		return x.score.size() > y.score.size();
}

 

由于数据超过100位,而long long最大值为2^63 - 1, unsigned long long最大值为2^64 - 1,故而只能以字符串形式存储输入数据。

 

代码如下:

// 票数可能会很大,可能会到100位数字

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

const int maxn = 25;

struct person
{
	int id;
	string score;
};

struct person s[maxn];

bool cmp(person x, person y)
{
	if (x.score.size() == y.score.size())
		return x.score > y.score;
	else
		return x.score.size() > y.score.size();
}

int main()
{
	int m;
	while (cin >> m)
	{

		for (int i = 0; i <
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值