题目背景
宇宙总统竞选
题目描述
地球历公元6036年,全宇宙准备竞选一个最贤能的人当总统,共有n个非凡拔尖的人竞选总统,现在票数已经统计完毕,请你算出谁能够当上总统。
输入输出格式
输入格式:
president.in
第一行为一个整数n,代表竞选总统的人数。
接下来有n行,分别为第一个候选人到第n个候选人的票数。
输出格式:
president.out
共两行,第一行是一个整数m,为当上总统的人的号数。
第二行是当上总统的人的选票。
输入输出样例
输入样例#1: 复制
5
98765
12365
87954
1022356
985678
输出样例#1: 复制
4
1022356
说明
票数可能会很大,可能会到100位数字。
n<=20
啊。结构体+字符串排序,太棒了。需要注意的点,string也提供了许多好用的函数,当然它自己也可以用ascll码比大小
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
const int maxn =1010;
using namespace std;
struct god{
string s;
int id;
} a[maxn];
int cmp(god a,god b){
if(a.s.size()==b.s.size())
return a.s>b.s;
else return a.s.size()>b.s.size();
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i].s,a[i].id=i;
sort(a+1,a+1+n,cmp);
cout<<a[1].id<<endl<<a[1].s;
return 0;
}
呃,这个不知道是谁写的半吊子比较,只能参考(滑稽)
#include <iostream>
#include <algorithm>
#include <cstdio>
const int maxn =1010;
using namespace std;
int main()
{
string max_="",now;
int n,id;
cin>>n;
for(int i=1;i<=n;i++){
cin>>now;
if(max_.length()<now.length()){
max_=now;
id=i;
}
else if(max_.length()==now.length()){
for(int j=0;i<max_.length();j++){
if(max_[j]<=now[j])
max_=now;
id=i;break;
}
}
}
cout<<id<<endl<<max_<<endl;
return 0;
}