Josephus使用数组进行问题

Josephus prob using arrays?

本文关键字:问题 数组 Josephus      更新时间:2023-10-16

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
    }
}    

如果我正确理解你,你想做的事情会是。。。

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[nhPerson]=0;这将从人员的总列表中删除第n个人员。