• 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
    %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
  • 相关阅读:
    2020/10/29
    2020/10/24
    2020/10/28
    2020/10/31周报
    linux shell 中判断字符串为空的正确方法
    20201107 千锤百炼软工人
    20201103 千锤百炼软工人
    20201109 千锤百炼软工人
    20201111 千锤百炼软工人
    20201105 千锤百炼软工人
  • 原文地址:https://www.cnblogs.com/cell-coder/p/12628623.html
Copyright © 2020-2023  润新知