洛谷——P1781 宇宙总统
生活随笔
收集整理的這篇文章主要介紹了
洛谷——P1781 宇宙总统
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目背景
宇宙總統(tǒng)競選
題目描述
地球歷公元6036年,全宇宙準(zhǔn)備競選一個最賢能的人當(dāng)總統(tǒng),共有n個非凡拔尖的人競選總統(tǒng),現(xiàn)在票數(shù)已經(jīng)統(tǒng)計完畢,請你算出誰能夠當(dāng)上總統(tǒng)。
輸入輸出格式
輸入格式:
?
president.in
第一行為一個整數(shù)n,代表競選總統(tǒng)的人數(shù)。
接下來有n行,分別為第一個候選人到第n個候選人的票數(shù)。
?
輸出格式:
?
president.out
共兩行,第一行是一個整數(shù)m,為當(dāng)上總統(tǒng)的人的號數(shù)。
第二行是當(dāng)上總統(tǒng)的人的選票。
?
輸入輸出樣例
輸入樣例#1:?復(fù)制
5 98765 12365 87954 1022356 985678輸出樣例#1:?復(fù)制
4 1022356說明
票數(shù)可能會很大,可能會到100位數(shù)字。
n<=20
?
AC:
#include <iostream> #include <cstring> #include <algorithm> using namespace std;int n; struct president {char piao[150];int num;int len;//票的位數(shù) };bool cmp(president a, president b) {if(a.len == b.len){for(int i = 0; i < a.len; i++){if(a.piao[i]-'0' > b.piao[i] - '0') return true;else if(a.piao[i]-'0' < b.piao[i] - '0') return false;else continue;}}else if(a.len > b.len) return true;else return false; } int main() {scanf("%d", &n); //候選人數(shù)struct president pre[n+1];for(int i = 1; i <= n; i++){scanf("%s", &pre[i].piao);pre[i].num = i;pre[i].len = strlen(pre[i].piao);}sort(pre+1, pre+n+1, cmp);printf("%d\n%s", pre[1].num, pre[1].piao);return 0; }?
總結(jié)
以上是生活随笔為你收集整理的洛谷——P1781 宇宙总统的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷——P1068 分数线划定
- 下一篇: 洛谷——P1583 魔法照片