这段代码有什么问题,被在线法官认定为错误答案?

时间:2021-07-24 12:26:51

标签: c++ c++14

#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,分别用RWC表示强> 分别,在大多数统计数据中比另一个更好的人被认为是更好的整体球员。判断 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”(不带引号)。

2 个答案:

答案 0 :(得分:3)

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

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

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

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

答案 1 :(得分:2)

<块引用>

这段代码有什么问题,被在线裁判判定为错误答案?

错误的部分是:

    x1 = r1 + w1 + c1;

    x2 = r2 + w2 + c2;

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