#correct answer but not
14 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
Problem
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 cumulative score for each player is calculated, and the leader and her current lead are found. 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
Note that the above table contains the cumulative scores.
The winner of this game is Player 1 as he had the maximum lead (58 at the end of round 1) during the game.
Your task is to help the Manager find the winner and the winning lead. You may assume that the scores will be such that there will always be a single winner. That is, there are no ties.
Input
The first line of the input will contain a single integer N (N ≤ 10000) indicating the number of rounds in the game. Lines 2,3,...,N+1 describe the scores of the two players in the N rounds. Line i+1 contains two integer Si and Ti, the scores of the Player 1 and 2 respectively, in round i. You may assume that 1 ≤ Si ≤ 1000 and 1 ≤ Ti ≤ 1000.
Output
Your output must consist of a single line containing two integers W and L, where W is 1 or 2 and indicates the winner and L is the maximum lead attained by the winner.
my answer
#include <iostream>
using namespace std;
int main() {
// your code goes here
int N;
int total1; int total2;
int lead1 = 0; int lead2 = 0;
cin >> N;
for(int i = 0; i < N; i++){
int p1; int p2;
cin >> p1 >> p2;
total1 += p1;
total2 += p2;
if((p1 - p2) > lead1){
lead1 = p1 - p2;
}
if((p2 - p1) > lead2){
lead2 = p2 - p1;
}
}
if(total1 > total2){
cout << 1 << " " << lead1 << endl;
}
if(total2 > total1){
cout << 2 << " " << lead2 << endl;
}
return 0;
}
but it does not work
- total1 and total2 are not initialised, in the Loop you use then +=, which is UB, bc total1/2 can be any value, so initialise it with =0;
- the Lead depends on the total number, so you may check the total1/2 in the loop rather than the round scores
- after the loop, you want to check the highest lead, not the highest total, bc "The winner of this game is Player 1 as he had the maximum lead (58 at the end of round 1) during the game."
if((p1 - p2) > lead1){
lead1 = p1 - p2;
}
//this is yours, but i think, that's what you need
if((total1 - total2) > lead1){
lead1 = total1 - total2;
}```
@violet stratus Has your question been resolved? If so, run !solved :)
@whole laurel thank you
np
This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.