codechef The Lead Game 题解

The game of billiards involves two players knocking 3 balls around

on a green baize table. Well, there is more to it, but for our

purposes this is sufficient.

The game consists of several rounds and in each round both players

obtain a score, based on how well they played. Once all the rounds

have been played, the total score of each player is determined by

adding up the scores in all the rounds and the player with the higher

total score is declared the winner.

The Siruseri Sports Club organises an annual billiards game where

the top two players of Siruseri play against each other. The Manager

of Siruseri Sports Club decided to add his own twist to the game by

changing the rules for determining the winner. In his version, at the

end of each round the leader and her current lead are calculated. Once

all the rounds are over the player who had the maximum lead at the

end of any round in the game is declared the winner.

Consider the following score sheet for a game with 5 rounds:

    Round     Player 1       Player 2

      1             140                 82
      2              89                 134
      3              90                 110
      4              112              106
      5              88                  90

The total scores of both players, the leader and the lead after

each round for this game is given below:

    Round      Player 1       Player 2     Leader     Lead

      1               140           	 82        Player 1     58
      2               229           	216       Player 1     13
      3               319           	326       Player 2      7
      4               431           	432       Player 2      1
      5               519           	522       Player 2      3

The winner of this game is Player 1 as he had the maximum lead (58

at the end of round 1) during the game.

根据规则计算最后的胜者是谁:当前局结束时候领先分数最多者胜。

本题依然是输入数据十分大。算法不难,处理好输入数据就行。都是使用fread和getchar比较稳妥了。

#include <stdio.h>

int TheLeadGame()
{
	int T, n, lead = 0, leader, a = 0, b = 0, c = 0, d = 0;
	scanf("%d\n", &T);
	char buffer[100000];
	bool one = true;
	if ((n = fread(buffer, 1, 100000, stdin)) > 0)
	{
		for (int i = 0; i < n; i++)
		{
			if (one && buffer[i] != ‘ ‘ && buffer[i] != ‘\n‘)
				a = a * 10 + buffer[i] - ‘0‘;
			else if (buffer[i] != ‘ ‘ && buffer[i] != ‘\n‘)
				b = b * 10 + buffer[i] - ‘0‘;

			if (buffer[i] == ‘ ‘) one = false;
			else if (buffer[i] == ‘\n‘ || i + 1 == n)
			{
				one = true;
				c += a;//player1 total scores
				a = 0;//reset
				d += b;//player2 total scores
				b = 0;//reset

				if (c < d)//calculate
				{
					if ( d - c > lead )
					{
						lead = d - c;
						leader = 2;
					}
				}
				else
				{
					if ( c - d > lead )
					{
						lead =  c - d;
						leader = 1;
					}
				}
			}
		}
	}
	printf("%d %d\n", leader, lead);
	return 0;
}

codechef The Lead Game 题解,布布扣,bubuko.com

时间: 2024-10-10 17:54:30

codechef The Lead Game 题解的相关文章

Codechef Maximum Weight Difference题解

Maximum Weight Difference Chef has gone shopping with his 5-year old son. They have bought N items so far. The items are numbered from 1 to N, and the item i weighs Wi grams. Chef's son insists on helping his father in carrying the items. He wants hi

codechef - Bytelandian gold coins 题解

In Byteland they have a very strange monetary system. Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to ma

codechef Ciel and Receipt题解

Tomya is a girl. She loves Chef Ciel very much. Tomya like a positive integer p, and now she wants to get a receipt of Ciel's restaurant whose total price is exactly p. The current menus of Ciel's restaurant are shown the following table. Name of Men

codechef The Morning Commute 题解

The Morning Commute The Chef commutes to work every day using the city's underground metro. The schedule for the trains has recently been changed and he wants to know how long it will take to travel from the station nearest to his house and the stati

codechef Jewels and Stones 题解

Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery accessories. She has been collecting stones since her childhood - now she has become really good with identifying which ones are fake and which ones are no

codechef Three Way Communications 题解

The Chef likes to stay in touch with his staff. So, the Chef, the head server, and the sous-chef all carry two-way transceivers so they can stay in constant contact. Of course, these transceivers have a limited range so if two are too far apart, they

Codechef Not a Triangle题解

找出一个数组中的三个数,三个数不能组成三角形. 三个数不能组成三角形的条件是:a + b < c 两边和小于第三边. 这个问题属于三个数的组合问题了.暴力法可解,但是时间效率就是O(n*n*n)了,很慢. 不过既然是组合问题就必定可以使用排序后处理的方法降低时间效率的. 这里降低时间效率的方法是: 选一个最大的数c,然后选两个小数a和b,其中a < b,如果符合条件,那么两个数中间的数必定都可以作为b符合条件a + b < c 这样可以把时间效率降到O(n*n). 这个规律也不好找啊.要

codechef Subtraction Game 1题解

Subtraction Game 1 Chef is playing a game on a sequence of N positive integers, say A1, A2, ... AN. The game is played as follows. If all the numbers are equal, the game ends. Otherwise Select two numbers which are unequal Subtract the smaller number

codechef Closing the Tweets 题解

Little kids, Jack and Evan like playing their favorite game Glass-and-Stone. Today they want to play something new and came across Twitter on their father's laptop. They saw it for the first time but were already getting bored to see a bunch of sente