入首先给出两个正整数 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
第一遍用java写的,运行最后两个超时,看来又是在卡大数据了,虽然之前在网上也查过java有时超时,而c语言就可以通过。看来java运行的还是要慢一些。
思路:外层循环是城市,内层循环是种类,设置一个种类大小的数组,一次输入销量,然后累加,在累加的过程中及时更新max的大小,最后再次遍历数组,当数组存放的销量等于max的时候就输出。
#include<stdio.h>
int main()
{
int N,M;
int max=-1;
int count=0;
int t;
scanf("%d %d",&N,&M);
int number[1000]={0};
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
scanf("%d",&t);
number[j]+=t;
if(number[j]>max)
max=number[j];
}
}
printf("%d\n",max);
for(int i=0;i<N;i++)
{
if(number[i]==max)
{
if(count!=0)
printf(" ");
printf("%d",i+1);
count++;
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int N,M;
int max=-1;
int count=0;
N=in.nextInt();
M=in.nextInt();
int number []=new int[N];
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
number[j]+=in.nextInt();
if(number[j]>max)
max=number[j];
}
}
System.out.println(max);
for(int i=0;i<N;i++)
{
if(number[i]==max)
{
if(count!=0)
System.out.print(" ");
System.out.print(i+1);
count++;
}
}
}
}