pat basic 1107 Rats love rice

Teacher Weng Kai once designed a Java challenge game called "Mouse Loves Rice" (perhaps because his nickname is "Fat Fat Mouse"). Each player uses Java code to control a mouse. The goal is to eat as much rice as possible to turn himself into a fat mouse. The fattest one is the champion.

Because the game time cannot be too long, we divide the players into N groups, with M rats in each group competing in the same field, and then directly select the fattest champion Fatty Rat from the N group champions. Now please write a program to get the champion's weight.

Input format:

The input gives 2 positive integers in the first line: N (≤100) is the number of groups, and M (≤10) is the number of players in each group. Then there are N lines, each line gives the final weight of M mice controlled by a group of players, all of which are non-negative integers not exceeding 104. Separate numbers with spaces.

Output format:

First, output the weight of each group champion in sequence on the first line. The numbers are separated by 1 space. There should be no extra spaces at the beginning and end of the line. Then the weight of the champion Fat Rat is output in the second line.

Input example:

3 5
62 53 88 72 81
12 31 9 0 2
91 42 39 6 48

Output sample:

88 31 91
91

Problem-solving ideas:

Two loops to find the maximum value

#include <stdio.h>

int main(int argc, const char *argv[]) {
	int i, j, N, M, maxN, maxM, tmp;
	if ( scanf("%d %d", &N, &M)==EOF ) printf("error\n");

	for ( maxN=i=0; i<N; ++i ) {
		for ( maxM=j=0; j<M; ++j ) {
			if ( scanf("%d", &tmp)==EOF ) printf("error\n");
			if ( maxM < tmp ) maxM = tmp;
		}
		if ( maxN < maxM ) maxN = maxM;
		printf("%d%c", maxM, i==N-1 ? '\n' : ' ');
	}
	printf("%d", maxN);

	return 0;
}

Guess you like

Origin blog.csdn.net/herbertyellow/article/details/126910043