打印特定范围内的阵列

时间:2019-02-07 19:49:28

标签: c++ c++14

我正在编写一个程序,内容如下: 从键盘读取20个数字,每个数字必须介于10到100之间(含10和100)。如果用户输入的数字超出预期范围,则必须反复要求输入新值,直到可接受为止。读取每个数字时,仅当它不是已读取的数字的副本时才打印。请注意,只要输入的数字在适当范围内,就可以输入20次。但是,如果发生这种情况,该数字将只打印一次,因为其他数字是重复的。 我遇到麻烦的部分是,只有输入了许多数字(当然在范围内),才打印一次数字。 这是我到目前为止的内容:

包括

int main()
{
    int SIZE{20};
    int myArray[SIZE]{0};

    for(int i{0}; i < SIZE; ++i)
    {
        while((myArray[i] < 10) || (myArray[i] > 100))
        {
            std::cout << "Enter a number: ";
            std::cin >> myArray[i];

            if(myArray[i] == myArray[i - 1])
            {
                std::cout << "Enter a number: ";
                std::cin >> myArray[i];
            }
        }
    }

    for(int i{0}; i < SIZE; ++i)
        std::cout << myArray[i] << " ";

    return 0;
}

2 个答案:

答案 0 :(得分:0)

一种实现方法是使用数组的所有值创建一个集合,然后仅打印该集合的值。 std::set对象将只为每个值保留一个唯一的实例。

#include <cstdio>
#include <string>
#include <iostream>
#include <set>

int main() {

  int SIZE{20};
  int myArray[SIZE]{0};

  for(int i{0}; i < SIZE; ++i)
  {
      while((myArray[i] < 10) || (myArray[i] > 100))
      {
          std::cout << "Enter a number: ";
          std::cin >> myArray[i];

          if(myArray[i] == myArray[i - 1])
          {
              std::cout << "Enter a number: ";
              std::cin >> myArray[i];
          }
      }
  }

  // Set declaration
  std::set<int> uniqueValues;
  for(int i{0}; i < SIZE; ++i)
      uniqueValues.insert(myArray[i]);

  // Printing set values
  for(auto itr = uniqueValues.begin(); itr != uniqueValues.end(); ++itr)
      std::cout << *itr << " ";

  return 0;
}

答案 1 :(得分:0)

简短答案-您的重复检查仅比较myArray[i]myArray[i - 1](因此它只会注意到连续的重复)。您需要将myArray[i]与从myArray[0]myArray[i - 1]的所有内容进行比较。但是还有其他问题,所以我写出了从头开始解决这个问题的方法。

让我们进一步分析问题,以便我们确定解决方案应包括的内容。此问题主要包括3个部分:

  1. 让用户输入20个数字
  2. 数字必须在10到100(含)之间。如果他们输入的数字无效,请不要减少剩余数字
  3. 剩余0个数字时,请打印输入的所有唯一数字。

首先-我们将重复输入过程,直到他们输入20个数字为止。听起来像是一个循环。

const int SIZE = 20;
int myArray[SIZE];
for (int i = 0; i < 20; ++i)
{
    std::cout << "\nEnter a number: ";
    std::cin >> myArray[i];
}

第二,该数字必须在10到100(含)之间。因此,仅当输入有效时,我们才增加索引。嗯,现在我们有了一个for循环,该循环在每次迭代时都会递增。让我们将其更改为while循环,并仅在其输入有效时才递增。

const int SIZE = 20;
int myArray[SIZE];
while (i < SIZE)
{
    std::cout << "\nEnter a number: ";
    std::cin >> myArray[i];
    if (myArray[i] >= 10 && myArray[i] <= 100)
        ++i;
}

下一步-当数字用完时,打印所有唯一数字。这是我的解决方案:

const int SIZE = 20;
int myArray[SIZE];
bool repeat = false;
int i = 0; //# of valid numbers entered so far
int j = 0; //array index
while (i < SIZE)
{
    std::cout << "\nEnter a number: ";
    std::cin >> myArray[j];
    //if the number was valid
    if (myArray[j] >= 10 && myArray[j] <= 100)
    {
        //check if it was unique.
        repeat = false;
        for (int k = 0; k < j; ++k)
        {
            if (myArray[k] == myArray[j])
            {
                repeat = true;
                break;
            }
        }
        //if the number was unique, increment the array index
        if (!repeat)
            ++j;
        ++i;
    }
}

//finally, print out every number in the array
for (int k = 0; k < j; ++k)
    std::cout << myArray[k] << std::endl;

我们分别跟踪其余条目(i)和数组索引(j)。如果他们的输入有效,则递增i。然后通过遍历索引0j - 1来检查该输入是否有效。如果发现重复,请将repeat标志设置为true并退出循环。检查完所有条目后,如果repeat仍然为false,则它是唯一值;递增j。当它们的条目用完时,输出从myArray[0]myArray[j - 1]的所有内容。

相关问题