0

http://en.wikipedia.org/wiki/Josephus_problem 用户可以选择圈子里有多少人。用户可以选择每个人的价值。用户可以选择人员死亡的计数。前任。用户选择5人,每5人死亡。

我在想类似的东西-用户选择人数前- 50 PeopleArray 变成 PeopleArray[50]

用户在 PeopleArray[50] 中选择元素的值 他们必须为 50 个元素键入 50 个值

死亡用户选择了 3,所以每三个人都会死,我将如何从数组中删除该数字。

问题^-不确定如何使用数组执行上述操作

    int main(){
    int people = 5;
    int peopleArray[5];
    int peopleValue = 1;
    int death;

    cout << "Enter the amount of people: ";
    cin >> people;

    peopleArray[people];        

    for(int x = 1;x<=people;x++){
        cout << "Enter the value of person #" << x << ":";
        cin>> peopleValue;
        peopleArray[peopleValue]; //Suppose to put the value into the array

    }
}    
4

1 回答 1

0

如果我正确理解你,你正在尝试做的事情就像......

vector<int> totalPeople;
int totalIn;
int deathIn;
int person = 1;
int nthPerson = 0;

cout << "Enter total number of people." << endl;
cin >> totalIn;
cout << endl << "Pick the nth person." << endl;
cin >> deathIn;

for ( int i = 0; i < totalIn; i++ )
{
  totalPeople.pushback(person++);
}
nthPerson = deathIn - 1;
while ( nthPerson < totalPeople.size() )
{
  totalPeople.erase(totalPeople.begin() + nthPerson);
  nthPerson = nthPerson + (deathIn -1);
}

或者说 totalPeople[nthPerson] = 0; 这将从总人员列表中删除第 n 个人。

于 2014-10-22T22:42:07.060 回答