PAT乙级:1092 最好吃的月饼 (20分)

PAT乙级:1092 最好吃的月饼 (20分)

题干

月饼是久负盛名的中国传统糕点之一,自唐朝以来,已经发展出几百品种。

fcb325a0-7090-4bf4-acb0-d4d7ea832f27

若想评比出一种“最好吃”的月饼,那势必在吃货界引发一场腥风血雨…… 在这里我们用数字说话,给出全国各地各种月饼的销量,要求你从中找出销量冠军,认定为最好吃的月饼。

输入格式:

输入首先给出两个正整数 N(≤1000)和 M(≤100),分别为月饼的种类数(于是默认月饼种类从 1 到 N 编号)和参与统计的城市数量。

接下来 M 行,每行给出 N 个非负整数(均不超过 1 百万),其中第 i 个整数为第 i 种月饼的销量(块)。数字间以空格分隔。

输出格式:

在第一行中输出最大销量,第二行输出销量最大的月饼的种类编号。如果冠军不唯一,则按编号递增顺序输出并列冠军。数字间以 1 个空格分隔,行首尾不得有多余空格。

输入样例:

5 3
1001 992 0 233 6
8 0 2018 0 2008
36 18 0 1024 4

      
    

输出样例:

2018
3 5

思路

  • pair<int,int>存储月饼的编号和总数量,放在vector里。
  • 将数据录入之后,排序
  • cmp函数按题意编写
  • 注意格式,输出方法可以参考下面的代码。

难得有一题时间上能比柳婼大神的题解快。

code

#include<unordered_map>
#include<vector>
#include<algorithm>
using namespace std;
bool cmp(pair<int,int> a,pair<int,int> b){
	if(a.second==b.second) return a.first<b.first;
	return a.second>b.second;
}
int main(){
	int type=0,city=0;
	scanf("%d%d",&type,&city);
	vector<pair<int,int>> sum(type);
	for(int i=0;i<type;i++) sum[i].first=i+1;
	for(int i=0;i<city;i++){
		for(int j=0;j<type;j++){
			int temp;
			scanf("%d",&temp);
			sum[j].second+=temp;
		}
	}
	sort(sum.begin(),sum.end(),cmp);
	printf("%d\n%d",sum[0].second,sum[0].first);
	for(int i=1;i<type;i++){
		if(sum[i-1].second==sum[i].second) printf(" %d",sum[i].first);
		else break;
	}
	return 0;
}

结果

提交时间 状态 分数 题目 编译器 耗时 用户
2020/4/3 18:58:56 答案正确 20 1092 C++ (g++) 14 ms a man
测试点 结果 耗时 内存
0 答案正确 5 ms 424 KB
1 答案正确 4 ms 360 KB
2 答案正确 3 ms 376 KB
3 答案正确 3 ms 364 KB
4 答案正确 13 ms 384 KB
5 答案正确 14 ms 424 KB

猜你喜欢

转载自www.cnblogs.com/cell-coder/p/12628623.html
今日推荐