【问题标题】:Whats wrong in this code , its been identified as Wrong answer by the online judge? [closed]这段代码有什么问题,被网上评委认定为错误答案? [关闭]
【发布时间】:2021-07-24 12:26:51
【问题描述】:
#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表示strong> 分别,在大多数统计数据中比其他人更好的人被认为是更好的整体球员。告诉 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”(不带引号)。

【问题讨论】:

标签: c++ c++14


【解决方案1】:

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

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

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

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

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

您可以引入“betterCounter”,然后将每个统计信息与其他统计信息进行比较。如果 A 的统计数据优于 B 的统计数据,则可以增加该值。

所以,我们需要比较。类似“c1 > c2”的东西。结果是一个布尔值。但非常方便的是,它可以转换为整数。为了让这个转换更清楚,我们可以将比较乘以 1。比如:(c1 &gt; 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;
}

【讨论】:

    【解决方案2】:

    这段代码有什么问题,被网上评委认定为错误答案?

    部分错误是:

        x1 = r1 + w1 + c1;
    
        x2 = r2 + w2 + c2;
    

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

    【讨论】:

    • 每个统计量意味着我们需要分别找到每个术语 r,w,c 的增量。非常有用。谢谢。
    • 然后将其存储在一个变量中。好的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多