編集:少なくともエラーを整理し、コードを更新したようです。しかし、数学はまだうまくいっていないようです。何か案は?
要するに、私は C++ でプログラムを作成しようとしています。このプログラムは、ユーザーに最初のサークル内の人数を入力するように促し、k (カウントされた人数が実行される前に) = 3.
正しい考えだと思いますが、kを 1、2、または 5 以外として入力すると、「Debug Assertion Failed」および「Expression: vector erase iterator outside range」というエラーが表示されます。
// 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;
}