这段代码有什么问题,被在线评委认定为Wrong answer?

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

#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;
}

这里的问题是

两个玩家 AB,表示为 R WC分别是,在大多数统计数据中比对方好的人被认为是整体上更好的球员。告诉 AB 谁更好。众所周知,在每一项统计中,球员都有不同的价值。

约束

1≤T≤1000

0≤R1, R2≤500

0≤W1, W2≤20

0≤C1, C2≤20

R1≠R2

W1≠W2

C1≠C2

预期输出:

对于每个测试用例,如果玩家 A 优于玩家 B,则在单行中输出“A”(不带引号),否则输出“B”(不带引号)。

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

错误的部分是:

    x1 = r1 + w1 + c1;

    x2 = r2 + w2 + c2;

它不测试哪个人is better than the other in most statistics。它总结了每个统计数据,但这并没有说明一个人是否在某个统计数据中更好,如果它在 one 统计数据中明显更好,它已经可以使一个人成为赢家。

我们随时为您提供帮助。我们可以在您的问题中找到 2 个子部分:

  1. 这段代码有什么问题?
  2. 为什么被在线评委认定为错误答案?

让我们先回答问题第1部分。所以,错误的是:

  • #include <bits/stdc++.h> 绝不能使用。它是一个非标准的 C++ 头文件。大多数编译器不知道。此外,它甚至不是 used/needed
  • #include <cmath> 不是 used/needed
  • using namespace std; 绝不能使用。相反,始终使用完全限定名称
  • ios_base::sync_with_stdio(false); 没有任何意义,在这种情况下根本没有必要
  • cin.tie(NULL); 没有任何意义,在这种情况下根本没有必要
  • 所有变量都应该是无符号的
  • 所有变量都应该有一个有意义的名字,例如。 “t”应命名为“numberOfTestCases”
  • 您可以考虑使用链式提取操作。而不是 6 行 cin >> ... 你可以只有一个语句 std::cin >> r1 >> w1 >> c1 >> r2 >> w2 >> c2;

那么,接下来是第2部分的问题。据我了解,您的解决方法是错误的。要想知道,哪个球员更好,就得一个一个的看数据了。

您可以引入“betterCounter”,然后将每个统计数据与另一个统计数据进行比较。如果 A 的统计数据比 B 的统计数据好,则可以增加此值。

所以,我们需要比较一下。类似于“c1 > c2”。结果是一个布尔值。但非常方便的是,它可以转换为整数。为了使这种转换更清楚,我们可以将比较值乘以 1。类似于:(c1 > C2) * 1.

然后我们可以想出如下解决方案('这是数百万种可能的实现之一)

#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;
}