【问题标题】:For loop returns multiple true statements how to return as one true bool varFor 循环返回多个 true 语句如何返回为一个 true bool var
【发布时间】:2019-08-16 08:54:40
【问题描述】:

我有一个 for 循环,它通过一个整数向量来找到一个特定的数字(6)和一个十位数字(1 或 2)。如果向量的所有整数都为真,我应该返回真,如果只有一个为假,我应该返回假并打印使其为假的元素。

在这种情况下,向量中的整数满足条件。但我不知道如何将三个 true 编译为一个可返回的 true

//    ∀x∈D,if the ones digit of x is 6, then the tens digit is 1 or 2.
// vector contains [-48, -14, -8, 0, 1, 3, 16, 23, 26, 32, 36]

void question5 (std::vector<int> x){

    int num;
    int digit1;
    int digit2;

    for (int i=0; i <x.size();i++){
        num = x.at(i); //stores the integer from the vector at position i
        digit1 = num % 10; // the ones digit is stored into digit1

        if (digit1 == 6){ // checks to see if the ones digit is 6
            digit2 = num/10; // if true gets the tens digit in num
            if ((digit1 ==6 && (digit2 == 1||2)))
                std::cout << "True"<< std:: endl;
            else
                std::cout << "False"<< std::endl;
        }

    }  
}

【问题讨论】:

  • std::all_of() 在这里会很有用,而不是循环。您是否了解标准算法或如何使用 lambda?
  • 感谢您提出问题。请包含一个 main() 函数并提供一个Compilable, Minimal, Complete, and Verifiable Example。这有助于我们帮助您。如果您可以在仍然创建故障的同时使您的问题尽可能简单,它将帮助我们隔离使您的代码正常工作的问题。通过提供一个 main() 函数,它可以为试图复制您的错误并为您提供帮助的人节省时间。
  • 您实际上并没有返回任何真实的陈述。您只需输出字符串“True”。为什么不只是一个布尔变量开始为假,一旦你将它设置为真,就打破循环。如果这太难了,那么计数器匹配了多少条件。如果 > 0,则打印“True”。

标签: c++ return boolean


【解决方案1】:

将三个条件编译成一个语句可以通过“and”或“&&”连接三个条件来完成。如果这三种情况都发生,则系统将失败,如下所示。整个向量检查无问题后,就说明你成功了。

我认为关于负数的问题存在歧义。正如所写,我相信 -36 作为第一个数字,将无法通过测试,但前提是您要注意负号:

#include <iostream>
#include <vector>

//    ∀x∈D,if the ones digit of x is 6, then the tens digit is 1 or 2.
// vector contains [-48, -14, -8, 0, 1, 3, 16, 23, 26, 32, 36]

void question5(std::vector<int> x)
{
  int i = 1;
  for (auto &number: x) {
    int abs_number = abs(number);
    if (abs(abs_number) % 10 == 6 and (abs_number / 10 != 1 and abs_number / 10 != 2)) {
      std::cout << "Element #" << i << " with a value of " << number
                << " is the first failing element." << std::endl;
      return;
    }
    i++;
  }
  std::cout << "All elements satisfy the criteria." << std::endl;
  return;
}

int main()
{
  std::vector<int> vect{-48, -14, -8, 0, 1, 3, 16, 23, 26, 32, 36};

  question5(vect);
  return 0;
}

【讨论】:

    【解决方案2】:

    但我不知道如何将三个 true 编译为一个可返回的 true

    1. 将函数的返回类型改为bool
    2. 第一次登录时返回 false
    3. 如果您从未遇到过false 条件,请在最后返回true
    4. 确保正确使用条件。 digit2 == 1||2 不正确。

    for (int i=0; i <x.size();i++){
        num = x.at(i); //stores the integer from the vector at position i
        digit1 = num % 10; // the ones digit is stored into digit1
    
        if (digit1 == 6){ // checks to see if the ones digit is 6
            digit2 = num/10; // if true gets the tens digit in num
    
            // No need for digit == 6 again. It's already been tested.
            // The correct way to test whether digit2 is 1 or 2.
            if ( (digit2 == 1) || (digit2 == 2) )
            {
                std::cout << "True"<< std:: endl;
            }
            else
            {
                std::cout << "False"<< std::endl;
                return false;
            }
        }
    }  
    
    return true;
    

    更简单的测试是计算 num % 100 并针对 1626 测试结果。

    for (int i=0; i <x.size();i++){
        num = x.at(i);
        int num2 = num % 100;
        if ( num2 == 16 || num2 == 26 )
        {
            std::cout << "True"<< std:: endl;
        }
        else
        {
            std::cout << "False"<< std::endl;
            return false;
        }
    }
    
    return true;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 2018-06-10
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多