洛谷P1781 宇宙总统【排序+字符串】

地球历公元6036年,全宇宙准备竞选一个最贤能的人当总统,共有n个非凡拔尖的人竞选总统,现在票数已经统计完毕,请你算出谁能够当上总统。

输入输出格式

输入格式:

president.in

第一行为一个整数n,代表竞选总统的人数。

接下来有n行,分别为第一个候选人到第n个候选人的票数。

输出格式:

president.out

共两行,第一行是一个整数m,为当上总统的人的号数。

第二行是当上总统的人的选票。

输入输出样例

输入样例#1: 复制

5
98765
12365
87954
1022356
985678

输出样例#1: 复制

4
1022356

说明

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

n<=20

思路:显然要用字符串来写,因为位数太多了肯定装不下,我用的string,用结构体数组排序选出票数最高的。另外也可以用字符串通过strcmp函数来比或者手写也可以。不过没有过,个人感觉评测姬有问题,起码样例是对的它都判wa,看看思路就好了……

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=5005;
struct presdient
{
    string str;
    int place,len;
};
bool cmp(presdient x,presdient y)
{
    if(x.len==y.len)
        return x.str>y.str;
    return x.len>y.len;
}
int main()
{
    struct presdient s[20];
    int n;
    scanf("%d",&n);
    getchar();
    for(int i=0;i<n;++i)
    {
        getline(cin,s[i].str);
        s[i].place=i+1;
        s[i].len=s[i].str.length();
    }
    sort(s,s+n,cmp);
    cout<<s[0].place<<endl<<s[0].str;
    //for(int i=0;i<n;++i)
      //  cout<<s[i].str<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/aerer/p/9930984.html