派对邀请
我现在正在处理的问题是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++中的数组不能是可变的,并且数组长度将是可变输入数组。任何帮助将非常感激。
我读过这个问题。您需要像链接列表这样的动态列表,因为您需要放置和删除不同索引中的不同项目,因此使用数组将很困难。
尝试使用std ::向量或std ::列表,你可以添加或删除任何任何列表
#include <list>
std::list<int> mylist;
的你怎么可以添加和从列表中删除值,检查此链接http://en.cppreference.com/w/cpp/container/list
对于使用自己的链表,检查此链接How could i create a list in c++?
我需要**一个动态列表,还是建议?考虑到对于一个从未超越基本数组,变量,for/while循环,if语句等类的作业而言,这是一个家庭作业问题,所以我认为不应该使用超出课程。 –
根据你的问题的std::vector
将是最好的选择,因为它是在原条款或我们阵列&链表的组合可以简单地说这是一个dynamic array
。
然而,正如你所提到的那样,n你的评论你没有被教过除基本数组以外的任何东西&想要在你学到的任何东西中找到解决方案,那么你必须采取array
。现在的问题是,数组是静态的&你不能从它删除元素。所以你所能做的就是保留一个能够处理其中元素数量的计数器。
// array insert and delete
#include <iostream>
using namespace std;
void see_array (int a[], int f, int b)
{
for (int i=f; i<b; ++i)
cout << a[i] << ' ';
cout << '\n';
}
int get_pos (int a[], int f, int b, int num)
{
for (int i=f; i<b; ++i)
{
if (a[i] == num)
return i;
}
return -1; // if search is unsuccessful
}
int main()
{
int a[] = {1,2,3,4,5,6,7,8,9,10};
int front = 0, back = 10; // as we have 10 elements
// delete last element
--back;
see_array(a, front, back);
// delete first element
++front;
see_array(a, front, back);
// delete element from middle say 6
int pos = get_pos(a, front, back, 6);
if (pos != -1)
{
for (int i = pos; i<back; ++i) // shift elements towards left
{
a[i] = a[i+1];
}
--back; // decrease size of the array
see_array(a, front, back);
}
return 0;
}
/* Output :-
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
2 3 4 5 7 8 9
*/
我希望上面的程序对你有些帮助!祝你好运 !!!
您需要更明确地提出您的问题(问题最后部分的“步骤是什么?”)。不用依靠外部链接就能更好地解释你的问题。 – Drop
“C++中的数组不能变”变量大小?那么,真假在同一时间。我认为在继续进行任何C++之前,您应该首先编写一本好书:[The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-和列表) – Drop
我知道输出是1,3,7,9,如果输入是10,2,2,3,但是我找不到用这个输入来得到这些数字的方法。我通常以10,10或7为单数。我认为我已经初始化了下来,现在只是公式和循环。这是我陷入困境的地方。此外,对于每个人都回应告诉我使用矢量或列表,这是一个教室没有教导的功课。我认为如果超出/ while循环,if语句,基本数组等等,我不认为它会被接受。 –