跳到正文

题解

题解 P1781 【宇宙总统】

Legends丶dream约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年2月3日
更新
2026年2月3日
篇幅
约 1 分钟
题目
P1781

版本

  1. v1当前

其实这里可以直接用快排。 直接代码里解释 sort的功能很强大,可以将大多数类型进行直接排序

C++
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>//sort的头文件
using namespace std;
int n;
struct point{
	int num;//用来存储号数;
	string s;//用来存储票数;使用string;
};
point a[25];
int cmp(const point& a,const point& b){
	if(a.s.length()>b.s.length()||(a.s.length()==b.s.length())&&a.s>b.s)return 1;//如果A的位数大于B或在位数相同的情况下字典序大于B;
	else return 0;
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i].s;
		a[i].num=i;//初始化
	}
	sort(a+1,a+n+1,cmp);
	cout<<a[1].num<<endl<<a[1].s;
	return 0;
}
原文