报告它在 C++ 上的现场

reporting the spot its at c++

本文关键字:现场 C++ 报告      更新时间:2023-10-16

我正在尝试一个程序来获取值的第一个,第二个,倒数第二个和最后一个位置

我的程序目前的运行方式是,它每次都会确定值的位置,而不是我想要的值。

//my code
#include<iostream>
using namespace std;
int main(){
  int number;
  int times;      
  int count=0;
  cout << "How many numbers will be entered?" << endl;
     cin >> times;
  do{
     cout << "Enter the number"<< endl;
     cin >> number;
     ++count;
      cout << count << endl;
     }
     while(times > count);
  if(number == 7 ){
     cout << "7 is in position: "<< count << endl;
     }
  return 0;
  }

您需要跟踪第一个和最后一个 7 个何时出现在循环之外。一种方法是在位置发生时存储位置。另一种方法是存储所有输入数字并在输入后计算位置。下面是一个示例:

#include<iostream>
using namespace std;
int main(){
    int number;
    int times;
    int count=0;
    int first = -1;
    int last = -1;
    cout << "How many numbers will be entered?" << endl;
    cin >> times;
    do {
         cout << "Enter the number"<< endl;
         cin >> number;
         ++count;
         if(number == 7) {
            if(first < 0)
                first = count;
            last = count;
         }
    }
    while(times > count);
    if(first >= 0) {
        cout << "The first 7 is in position: "<< first << endl;
        cout << "The last 7 is in position: "<< last << endl;
    }
    else {
        cout << "No 7 occurred." << endl;
    }
    return 0;
}

编辑:当我解决这些需要一个或多个特定值的问题时,我通常首先在函数中保存所需值的任何其他代码之前创建变量。在这种情况下,目标是有两个我可以访问的数字,它们表示第一次出现7和最后一次出现7,因此我创建了整数变量firstlast

我要做的下一件事是将变量设置为"无效"的值。也就是说,我希望能够在函数中的任何一点查看它们,并判断是否出现了一个或多个7。通过将firstlast设置为-1,我知道只要它们以这种方式设置,就不会出现7,因为如果有的话,它们将被更新到1的最小位置。

现在我需要弄清楚获取我想要的值的最佳位置在哪里。代码唯一知道输入值及其位置的时间是在++count之后的do...while循环中,因此它似乎是最好的起点。通过添加if(number == 7)我可以定义一些仅在出现7时执行的代码。下一个if检查是否尚未设置first,或者换句话说,它无效,因为它小于 0 。如果是这种情况,我们知道出现了7,应该更新first。最后,last将始终设置,因为据我们所知,出现的7可能是最后一个出现的。

cout s 的最后一个更改有一个 if 语句,用于检查first是否无效(不是 -1)。如果是这种情况,则first已设置(已出现7),因此必须已设置last。如果未设置first则不会出现7

正如您所描述的,问题在于您只关心7第一次最后一次出现。当您的应用程序首次启动时7从未输入过,最终可能会输入数据,并且可能会产生以下结果:

  • 从不输入7
  • 7输入一次(即,这将是第一次最后一次出现)
  • 多次输入7(即,跟踪第一个匹配项并为每个后续7更新最后一个匹配项

示例代码

另请参阅代码注释。

#include <iostream>
int main()
{
    int firstPos = -1;
    int lastPos = -1;
    std::cout << "How many numbers will be entered? ";
    int numbersToEnterCount;
    std::cin >> numbersToEnterCount;
    for (int i = 0; i < numbersToEnterCount; ++i)
    {
        std::cout << "Enter num: ";
        int number;
        std::cin >> number;
        if (number == 7)
        {
            // This is true only when 7 has never been entered
            if (firstPos == -1)
            {
                // Set the first position, doing this will also prevent it
                // from being updated anymore
                firstPos = i + 1;
            }
            // Always update the last position
            lastPos = i + 1;
        }
    }
    if (firstPos != -1)
    {
        std::cout << "The first 7 was in position " << firstPos << "n";
        std::cout << "The last 7 was in position " << lastPos << "n";
    }
    else
    {
        std::cout << "7 was never enteredn";
    }
    return 0;
}