My issue
My code
// Update the code below to solve the problem
#include <iostream>
#include <string>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int A,B;
cin>>A>>B;
if(A==0 && B==0)
{
cout<<7<<endl;
}
else{
cout<<min(A,B)<<endl;
}
}
return 0;
}
Learning course: Solve Programming problems using C++
Problem Link: CodeChef: Practical coding for everyone
First let me explain what your code does:
When the else block is executed, the compiler outputs the value of score that is least among the two values. So, if Alice has 3 points and Bob has 4 points the output according to your code is 3 (which in this case is even the right answer
).
But if Alice has 5 points and Bob has 4 points the output will be 4 and here your answer will come out to be wrong.
Now coming to the demand of the problem statement:
The problem demands us to find “the minimum number of points that will be further scored in the match before it ends.”
So, suppose the problem where Alice has 5 points and Bob has 4 points. The match ends when either Alice or Bob scores 7 points. Now, Alice needs 2 more points to win the game whereas Bob needs 3 more points. If we want to end this game as soon as possible, Alice needs to score 2 more points consecutively.
Hence the formula that will be used here will be 7-max(A,B).
Note that this formula accommodates all the possible cases ranging from 0 to 7 and therefore you don’t even need to use if-else statement!!!
Even if the score is 0-0 the minimum number of points required to finish this game will be 7 (either Alice score 7 consecutive points or Bob score 7 consecutive points).
So the final code would look like this
#include <iostream>
#include <string>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int A,B;
cin>>A>>B;
cout<<7-max(A,B)<<endl;
}
return 0;
}
Hope I answered your problem.
Cheers !!!``
1 Like
@ruby_stacy
You would need to calculate the maximum value between A and B, and print its difference from seven.
Here is my code for reference.
// Update the code below to solve the problem
#include <iostream>
#include <string>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int a,b;
cin>>a>>b;
if(a>b)
{
cout<<(7-a)<<endl;
}
else
{
cout<<(7-b)<<endl;
}
}
return 0;
}
1 Like