c++ - 聚会请柬

标签 c++ arrays

我现在正在处理的问题是 here ,但我当然不是在寻找这个作业问题的完整答案。我所要求的只是问题最后部分的步骤。这是我目前所拥有的:

int main()
{
    cout << "Please enter the number of guests attending your party: ";
    int k;
    cin >> k;
    cout << "Please enter the number of rounds of removal you'd like to perform: ";
    int m;
    cin >> m;
    for (int i = 1; i <= m; i++) {
        cout << "Please enter the multiple at which you'd like the removal to be at for round " << i << ": ";
            int r;
            cin >> r;
                if (k % r == 0) {
                    k - r;
                }
                cout << k << endl;
    }
    system("pause");
}

这一切让我很困惑,我真的不知道该去哪里寻找答案。似乎我需要一个数组来解决这个问题,但 C++ 中的数组不能是可变的,并且数组长度将是 k,一个可变输入。任何帮助将不胜感激。

最佳答案

我读过那个问题。你需要一个像链表这样的动态列表,因为你需要从不同的索引中放置和删除不同的项目,所以使用数组会很困难。

尝试使用 std::vector 或 std::list,你可以添加或删除任何列表

#include <list>
std::list<int> mylist;

如何从列表中添加和删除值,查看此链接 http://en.cppreference.com/w/cpp/container/list

要使用您自己的链接列表,请查看此链接 How could i create a list in c++?

关于c++ - 聚会请柬,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34915539/

相关文章:

C++ 自定义容器传递 {} 列表

javascript - 将使用 range 的 Coffeescript 循环转换为 ES6

c++ - 为什么友元运算符<<在某些情况下使用而在其他情况下不使用

C++ 字符串转类名

c++ - 为什么 C++ OBJ 文件很重要?

c++ - ROS 移动约束

C++ 段错误

java - 无法将 int 添加到 ArrayList 的 ArrayList 中

xml - 将两个数组中的 XML 数据相加

php - 将变量值与 PHP 中的查询结果进行比较