-5
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>

using namespace std;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;

  int w1, c1, r1, w2, c2, r2;
  int x1 = 0;
  int x2 = 0;

  cin >> t;

  for (int i = 0; i < t; i++) {

    cin >> r1;
    cin >> w1;
    cin >> c1; // t=0 test case-1

    cin >> r2;
    cin >> w2;
    cin >> c2;

    x1 = r1 + w1 + c1;

    x2 = r2 + w2 + c2;

    if (x1 > x2) {

      cout << "A" << endl;

    }

    else
      cout << "B" << endl;
  }

  return 0;
}

The question here is

two players A and B, denoted by R, W, and C respectively, the person who is better than the other in most statistics are regarded as the better overall player. Tell who is better amongst A and B. It is known that in each statistic, the players have different values.

Constraints

1≤T≤1000

0≤R1, R2≤500

0≤W1, W2≤20

0≤C1, C2≤20

R1≠R2

W1≠W2

C1≠C2

Expected output:

For each test case, output in a single line "A" (without quotes) if player A is better than player B and "B" (without quotes) otherwise.

t.niese
  • 39,256
  • 9
  • 74
  • 101
Henry Worxlor
  • 23
  • 1
  • 5
  • 1
    Are R, W, and C the statistics? If so, you're computing their sum instead of finding how many each player is better at. – interjay Jul 24 '21 at 12:35
  • 1
    What happened when you tried solving a sample problem by hand, and comparing it to the result you get from your program? – Karl Knechtel Jul 24 '21 at 12:36
  • 4
    [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h), [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – t.niese Jul 24 '21 at 12:40
  • @KarlKnechtel I will try tracing from next time. Appreciated. – Henry Worxlor Jul 24 '21 at 15:39

2 Answers2

3

We are here to help. We can find 2 sub parts in your question:

  1. Whats wrong in this code?
  2. Why its been identified as wrong answer by the online judge?

Let us first answer the question part no 1. So, wrong is:

  • #include <bits/stdc++.h> should never be used. It is a non standard C++ header file. Not known by most compilers. Additionally, it is even not used/needed
  • #include <cmath> is not used/needed
  • using namespace std; should never be used. Instead, always use fully qualified names
  • ios_base::sync_with_stdio(false); has no meaning and is not necessary at all in this context
  • cin.tie(NULL); has no meaning and is not necessary at all in this context
  • all variables should be unsigned
  • all variables should have a meaningful name, eg. "t" should be named "numberOfTestCases"
  • You could consider the usage of a chained extraction operation. Instead of the 6 lines cin >> ... you could have only one statement std::cin >> r1 >> w1 >> c1 >> r2 >> w2 >> c2;

Then, next, question part no 2. In my understanding, your solution approach is wrong. If you want to know, which player is better, you have to look at the statics one by one.

You could introduce a "betterCounter" and then compare each statistic with the other statistic. And if statistic from A is better than that from B, you could increment this value.

So, we need to compare. Something like "c1 > c2". The result of that is a boolean value. But very conviently, it can be converted to an integer. And to make this conversion clear, we could multiply the comparison with 1. Something like: (c1 > C2) * 1.

Then we could come up with a solution like the following (´this is one of millions of possible implementations)

#include <iostream>

int main() {

    // Get the number of testcases
    unsigned int numberOfTestCases{};
    std::cin >> numberOfTestCases;

    // Now operate all test cases
    while (numberOfTestCases--) {

        // Definition of statistic variables
        unsigned int playerA_StatisticR{}, playerA_StatisticW{}, playerA_StatisticC{},
                     playerB_StatisticR{}, playerB_StatisticW{}, playerB_StatisticC{};

        // Read all values
        std::cin >> playerA_StatisticR >> playerA_StatisticW >> playerA_StatisticC >>
            playerB_StatisticR >> playerB_StatisticW >> playerB_StatisticC;

        // Calculate result for player A and player B
        const unsigned int sumPlayerAisBetter = (playerA_StatisticR > playerB_StatisticR) * 1 +
            (playerA_StatisticW > playerB_StatisticW) * 1 +
            (playerA_StatisticC > playerB_StatisticC) * 1;

        const unsigned int sumPlayerBisBetter = (playerB_StatisticR > playerA_StatisticR) * 1 +
            (playerB_StatisticW > playerA_StatisticW) * 1 +
            (playerB_StatisticR > playerA_StatisticC) * 1;

        // Show result
        if (sumPlayerAisBetter > sumPlayerBisBetter ) std::cout << "A\n";
        if (sumPlayerBisBetter > sumPlayerAisBetter ) std::cout << "B\n";
    }
    return 0;
}
A M
  • 14,694
  • 5
  • 19
  • 44
2

Whats wrong in this code , its been identified as Wrong answer by the online judge?

The part is wrong is:

    x1 = r1 + w1 + c1;

    x2 = r2 + w2 + c2;

It does not test which person is better than the other in most statistics. It sums up each statistic, but that does not say anything about if one person was better in a certain statistics, it could already make one person the winner if it is significantly better in one statistics.

t.niese
  • 39,256
  • 9
  • 74
  • 101