【问题标题】:simple statistics implementation in c++C++中的简单统计实现
【发布时间】:2021-09-02 11:03:11
【问题描述】:

说明

先生。 Dengklek 有你 N 个整数。在这些数字中,确定最大和最小的数字。

输入格式

第一行包含一个整数 N。下一行包含 N 个整数。

输出格式

A 行包含 A B,其中 A 为最大数,B 为最小数。

输入示例

8

1 -1 1 10 10 6 8 4

示例输出

10 -1

限制

1≤N≤100

-100,000 ≤(第二行的每个整数)≤100,000

#include <iostream>
using namespace std;
int main(){
  int N,A,B, temp;
  while (true)
  {
    cin >> N;
    if(N > 0 && N <= 100){
      break;
    }
  }

  for (int i = 0; i < N; i++)
  {
    cin >> temp;
    if(temp >= -100000 && temp <= 100000){
      if(temp < A && temp < B){
        B = temp;
      }
      else if(temp > B && temp > A){
        A = temp;
      }
    }
    else{
      i--;
    }
    
  }
  cout << A << endl;
  cout << B << endl;
  return 0;
  
  
}

我在这个问题中得到了 WA 和 0 分,你能帮我找出问题所在吗?我一直在测试这个程序并且它运行良好

【问题讨论】:

  • 什么是WA?程序究竟是如何获得“积分”的?

标签: c++ algorithm for-loop statistics c++17


【解决方案1】:

您在未初始化的情况下使用了变量 AB。这种未初始化的局部变量的初始值是不确定的。您必须添加一些代码以不使用此类不确定的值。

例如:

#include <iostream>
using namespace std;
int main(){
  bool AB_valid = false; // add a flag to indicate if A and B has a valid value
  int N,A,B, temp;
  while (true)
  {
    cin >> N;
    if(N > 0 && N <= 100){
      break;
    }
  }

  for (int i = 0; i < N; i++)
  {
    cin >> temp;
    if(temp >= -100000 && temp <= 100000){
      if(AB_valid){ // check the flag
        // if A and B contains valid value, use them for calculation
        if(temp < A && temp < B){
          B = temp;
        }
        else if(temp > B && temp > A){
          A = temp;
        }
      }
      else{
        // if they doesn't contain valid value, assign the first value
        A = temp;
        B = temp;
        AB_valid = true;
      }
    }
    else{
      i--;
    }
    
  }
  cout << A << endl;
  cout << B << endl;
  return 0;
  
  
}

【讨论】:

  • @MikeCat,我想问 1 个问题。第一次执行“if(temp
  • 没有。在我的代码中,if(temp &lt; A &amp;&amp; temp &lt; B) 将在某些值分配给 AB 后执行,这要感谢检查 if(AB_valid)
【解决方案2】:

MikeCAT 的回答绝对正确。 OP 的问题是由于 AB 没有正确初始化,导致“未确定的行为”。

只是为了好玩,我想提供一个类似的解决方案,它不需要额外的变量“bool AB_valid”。在这个解决方案中,您可以简单地在函数开头直接初始化 AB 的值。

我了解到每个变量都必须正确初始化,并且为了清楚起见,还应该在单独的一行中列出

#include <iostream>

int main(){
  
  const int MAX = 100000;
  const int MIN = -100000;
  
  int N = 0;
  int A = MIN;
  int B = MAX;
  int temp = 0;
  
  while (true)
  {
    std::cin >> N;

    if(N > 0 && N <= 100){
      break;
    }
  }

std::cout << "\n ***  ";

  for (int i = 0; i < N; i++)
  {
    std::cin >> temp;
    if(temp >= MIN && temp <= MAX){
    
        // if A and B contains valid value, use them for calculation
        if( temp < B){
          B = temp;
        }
        
        if( temp > A){
          A = temp;
        }
    }        
    else
    {
      i--;
    }
    
  }

  std::cout << "\n Max = " << A ;
  std::cout << "\n Min = " << B ;

  return 0;  
  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 2011-06-03
    • 2010-11-09
    • 1970-01-01
    • 2010-09-20
    • 2022-12-05
    相关资源
    最近更新 更多