PAT Basic 1092. 最好吃的月饼
PAT Basic 1092. 最好吃的月饼
1. 题目描述:
月饼是久负盛名的中国传统糕点之一,自唐朝以来,已经发展出几百品种。
若想评比出一种“最好吃”的月饼,那势必在吃货界引发一场腥风血雨…… 在这里我们用数字说话,给出全国各地各种月饼的销量,要求你从中找出销量冠军,认定为最好吃的月饼。
2. 输入格式:
输入首先给出两个正整数 \(N\)(\(≤1000\))和 \(M\)(\(≤100\)),分别为月饼的种类数(于是默认月饼种类从 1 到 \(N\) 编号)和参与统计的城市数量。
接下来 \(M\) 行,每行给出 \(N\) 个非负整数(均不超过 1 百万),其中第 \(i\) 个整数为第 \(i\) 种月饼的销量(块)。数字间以空格分隔。
3. 输出格式:
在第一行中输出最大销量,第二行输出销量最大的月饼的种类编号。如果冠军不唯一,则按编号递增顺序输出并列冠军。数字间以 1 个空格分隔,行首尾不得有多余空格。
4. 输入样例:
5 3
1001 992 0 233 6
8 0 2018 0 2008
36 18 0 1024 4
5. 输出样例:
2018
3 5
6. 性能要求:
Code Size Limit
16 KB
Time Limit
200 ms
Memory Limit
64 MB
思路:
定义二维数组mooncake
统计各个城市每种月饼的销量即可,这里题目指出每行给出的月饼销量不超过\(10^6\),本来以为月饼销量求和会超出int类型的最大值进而需要额外处理,但是因为城市数量\(M \le 100\),所以月饼销量总和的最大值为\(10^8\),小于\(10^9\),int类型是存的下的,直接进行统计求和即可。第一次提交时testpoint1,4,5报wrong answer,检查后发现弄错了最大销量的题意,本来以为是找出\(M \times N\)个销量中的最大值,被输入样例中的2018误导了。。。
My Code:
#include <stdio.h>
#define MAX_KIND 1000
#define MAX_CITY (100+1)
// first submit testpoint1, 4, 5 wrong answer, for I misunderstand the maxSold.
int main(void)
{
int mooncake[MAX_CITY][MAX_KIND] = {0};
int kindCount=0, cityCount=0;
//int maxSold=0,
int championCake=0;
int i=0, j=0; // iterator
int firstBlood=0; // output space flag
scanf("%d%d", &kindCount, &cityCount);
//maxSold = -1;
for(i=1; i<=cityCount; ++i)
{
for(j=0; j<kindCount; ++j)
{
scanf("%d", &mooncake[i][j]);
//if(mooncake[i][j] > maxSold) maxSold = mooncake[i][j];
}
}
for(i=1; i<=cityCount; ++i) // sum the sold of first kind
{
mooncake[0][0] += mooncake[i][0];
}
championCake = mooncake[0][0];
for(j=1; j<kindCount; ++j) // sum the sold of other kind
{
for(i=1; i<=cityCount; ++i)
{
mooncake[0][j] += mooncake[i][j];
}
if(mooncake[0][j] > championCake) championCake = mooncake[0][j];
}
printf("%d\n", championCake);
for(j=0; j<kindCount; ++j)
{
if(mooncake[0][j] == championCake)
{
if(firstBlood)
{
printf(" %d", j+1);
}
else
{
printf("%d", j+1);
firstBlood = 1;
}
}
}
printf("\n");
return 0;
}