编辑:我似乎至少已经解决了错误,并更新了代码。但是,数学似乎仍然没有解决。有任何想法吗?
简而言之,我正在尝试用 C++ 编写一个程序,它会提示用户输入初始圆圈中的人数,然后告诉他们如果k(计算的人数在被执行之前)= 3。
我有我认为正确的想法,但是如果我将k输入为 1、2 或 5 以外的任何值,则会收到错误“调试断言失败”和“表达式:向量擦除迭代器超出范围”。
// ConsoleApplication2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int n;//size of the circle
vector <int> circle; //the circle itself
//ask for how many people are in the circle
cin >> n;
//fill the circle with 1,2,3...n
for (int idx = 0; idx < n; idx++)
{
circle.push_back (idx+1);
}
//cout << "The size of the circle is " << circle.size() << ".\nThe highest number is " << circle[n-1] << "."; //test to make sure numbers are being assigned properly to each vector element
for (int count = 0, idx = 0; circle.size() > 1; idx++,count++)
{
//if the position (idx) is greater than the size of the circle, go back to the beginning of the circle and start counting again
if (idx >= circle.size())
{
idx = 0;
}
//every time the counter reaches three, that person is executed
if (count == 3)
{
circle.erase (circle.begin()+(idx));
count = 0;
}
}
cout << "The place to stand to win the hand is position #" << circle.front() << ".\n";
return 0;
}