pat basic 1102 teaching super champion paper

"Education Supermarket" is a derivative product of the puzzle A system, which releases various test papers and exercises for users to purchase. In the test paper list, the system not only lists the unit price of each test paper, but also displays the current number of purchases. This question asks you to use this information to find the sales volume (ie number of purchases) champion and sales champion among all the test papers in the education supermarket.

Input format:

The input first gives a positive integer N (≤104) in the first line, followed by N lines, each line gives a unique ID of a paper (a string consisting of lowercase letters and numbers, with a length of no more than 8 characters) , unit price (a positive integer not exceeding 100) and number of purchases (a non-negative integer not exceeding 106).

Output format:

Output the ID of the sales champion and his sales volume in the first line, and output the ID of the sales champion and his sales volume in the second line. Lines of output are separated by a space. The title guarantees that the winner will be unique and there will be no tie.

Input example:

4
zju007 39 10
pku2019 9 332
pat2018 95 79
qdu106 19 38

Output sample:

pku2019 332
pat2018 7505

Problem-solving ideas:

Read the data normally and find the maximum value. There is a small pitfall that you should pay attention to. You must use the first piece of data to initialize the results, otherwise test point 1 will overturn.

        

#include <stdio.h>
#include <string.h>
#define MAXN 10

typedef struct node {
	char id[MAXN];
	int price, amount, revenue;
} Paper;

int main(int argc, const char *argv[]) {
	int N;
	Paper temp, max_amount, max_revenue;
	if ( scanf("%d", &N)==EOF ) printf("error\n");

	if ( scanf("%s %d %d", temp.id, &temp.price, &temp.amount)==EOF ) printf("error\n");
	temp.revenue = temp.price * temp.amount;
	strcpy(max_amount.id, temp.id);
	max_amount.amount = temp.amount;
	strcpy(max_revenue.id, temp.id);
	max_revenue.revenue = temp.revenue;
	while ( --N ) {
		if ( scanf("%s %d %d", temp.id, &temp.price, &temp.amount)==EOF ) printf("error\n");
		temp.revenue = temp.price * temp.amount;
		if ( max_amount.amount < temp.amount ) {
			strcpy(max_amount.id, temp.id);
			max_amount.amount = temp.amount;
		}
		if ( max_revenue.revenue < temp.revenue ) {
			strcpy(max_revenue.id, temp.id);
			max_revenue.revenue = temp.revenue;
		}
	}

	printf("%s %d\n%s %d\n", max_amount.id, max_amount.amount, max_revenue.id, max_revenue.revenue);

	return 0;
}

Guess you like

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