【问题标题】:C++ trying to count how many times a user has entered a specific numberC ++试图计算用户输入特定数字的次数
【发布时间】:2021-05-29 10:54:51
【问题描述】:

我正在尝试编写一个程序来计算用户从 while 循环中输入特定数字的次数。思路如下:

#include <iostream>
#include <ctime>
using namespace std;

int main() {
    bool progLoop = true;
    int Number;
    char response;
    while (progLoop == true)
    {
        cout << "Please enter a number: " << endl; \
        cin >> Number;
        cout << "Number collected!" << endl;
        cout << "Number of times that number has been entered: " << endl; //IDK how to do this part which is what I'm looking for.
        cout << "Do you want to enter another number?" << endl;
        cin >> response;
        if (response == 'y') 
        {
            progLoop == true;
        }
        else if(response == 'n')
        {
            progLoop == false;
        }
    }
}

我正在寻找一种方法来存储特定数字已输入程序的次数的值。如果有任何问题想澄清一下!谢谢! (修改我的代码会很棒!)

【问题讨论】:

  • 一种方法是创建一个向量,每次输入新数字时,都会将其推回向量中。然后您可以在需要检查时std::count该号码。另一种方法是使用std::unordered_map
  • 顺便说一句,你不小心在循环中写了progLoop == true;progLoop == false;,这意味着即使你输入n,循环仍然会运行。
  • 我刚刚发布了使用 unordered_map 的方式,恕我直言,性能更好
  • 您不需要布尔值,只需将字符集设置为 'y'
  • 你也应该有括号之间的用法(y/n)

标签: c++ count numbers


【解决方案1】:

将我的评论扩展为答案

这样做的一种方法是使用std::vector,每次输入新数字时,都会将其推回向量中。然后您可以在需要检查时std::count该号码。在你的程序中,它看起来像这样:

#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;

int main() {
    bool progLoop = true;
    int Number;
    std::vector<int> numCount;
    char response;
    while (progLoop == true)
    {
        cout << "Please enter a number: " << endl;
        cin >> Number;
        numCount.push_back(Number);
        cout << "Number collected!" << endl;
        cout << "Number of times that number has been entered: " << std::count(numCount.begin(), numCount.end(), Number) << endl;
        cout << "Do you want to enter another number?" << endl;
        cin >> response;
        if (response == 'y') 
        {
            progLoop = true; // You accidentally used == here
        }
        else if(response == 'n')
        {
            progLoop = false; // You accidentally used == here
        }
    }
}

【讨论】:

    【解决方案2】:

    使用hashmap

    #include <unordered_map>
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    int main() {
        int number;
        char response = 'y';
        unordered_map<int, int> m; // your hashmap
    
        while (response == 'y')
        {
            cout << "Please enter a number: " << endl; 
            cin >> number;
            cout << "Number collected!" << endl;
            cout << "Number of times that number has been entered: " << ++m[number] << endl; // preincrement your hashmap at key number
            cout << "Do you want to enter another number? (y/n)" << endl;
            cin >> response;
            response = tolower(response); // give a chance to exit the loop if your user like to use caplock (like Trump)
        }
    }
    

    【讨论】:

    • 在这种情况下,我要么 1) 摆脱 m[Number]++; 并改用 ... &lt;&lt; ++m[Number] &lt;&lt; ...; 2) 将m[Number]++; 更改为int count = ++m[Number]; 并改用... &lt;&lt; count &lt;&lt; ...
    • @Antonin GAVREL 你的代码中还有progLoop ==...。它不会起作用。
    • 已编辑(nb:不需要计数),布尔值都没有
    猜你喜欢
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多