PAT-B 1092 The best moon cakes (20 points)

1092 The best moon cakes (20 points)

Moon cakes are one of the most prestigious traditional Chinese pastries. Since the Tang Dynasty, hundreds of varieties have been developed.
If you want to compare the "best" mooncakes, it will inevitably cause a bloody storm in the food industry... Here we use numbers to give the sales of various mooncakes across the country, and ask you to find the sales champion from it. Recognized as the most delicious moon cake.

Input format: The
input first gives two positive integers N (≤1000) and M (≤100), which are the number of mooncake types (so the default mooncake types are numbered from 1 to N) and the number of cities participating in the statistics.
In the next M rows, each row gives N non-negative integers (none of which exceeds 1 million), where the i-th integer is the sales volume (block) of the i-th moon cake. Numbers are separated by spaces

Output format:

The largest sales volume is output in the first row, and the type number of the mooncake with the largest sales volume is output in the second row. If the champion is not unique, the tied champions will be output in ascending order. The numbers are separated by 1 space, and there must be no extra spaces at the beginning and end of the line.

Input sample:

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

Idea:
Use structure definition. The structure variable is an array, which is used to store the cities participating in the voting. The structure member is an array of moon cakes.

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct test
{
    
    
int mooncake[1001] = {
    
     0 };

}city[101] = {
    
    0};


int main()
{
    
       
	int count = 0;
	int moon_cake[1000] = {
    
     0 };
	int M,N;			 					//M表示城市个数.N表示月饼的种类数
	int maxn = -1000,id=0;

	cin>>N>>M ;
	int temp = N;
	int sum[1001] = {
    
    0};
											//接下来 M 行,每行给出 N 个非负整数
	for (int i = 0; i < M; i++)	
	{
    
    
		for (int j = 1; j <= N; j++)
		{
    
    
			cin >> city[i].mooncake[j];
			sum[j]+=city[i].mooncake[j];  	 //计算每个种类在不同城市得到销量
		}
	}

	for (int i = 1; i <= N; i++)
	{
    
    
		if (maxn <sum[i])
		{
    
    
			maxn = sum[i];  //获得最大值
		}
	}

	cout << maxn << endl;
    
	for (int i = 1; i <= N; i++)   			 //控制格式,用count来控制最后的空格输出
	{
    
    
		if (maxn == sum[i])
		{
    
    
			count++; 
		}
	}

	for (int i = 1; i <= N; i++)
	{
    
    
		if (maxn == sum[i])
		{
    
    
			if (count ==1) printf("%d\n",i);    //遇到倒数第一个数就直接换行。
			else printf("%d ", i);
			count--;
		}
	}
}

Insert picture description here

Guess you like

Origin blog.csdn.net/SKMIT/article/details/113832607