0

http://en.wikipedia.org/wiki/Josephus_problem ユーザーはサークル内の人数を選択できます。ユーザーは、各人の値を選択できます。ユーザーは、人の死亡のカウント数を選択できます。元。ユーザーが 5 人を選ぶと、5 人に 1 人が死亡します。

私は次のようなことを考えていました - ユーザーは人数を選択します ex- 50 PeopleArray は PeopleArray[50] になります

ユーザーは PeopleArray[50] の要素の値を選択します。50 個の要素に対して 50 個の値を入力する必要があります。

Death User は 3 を選ぶので、3 人に 1 人が死ぬので、どうすればその数字を配列から消去できますか。

問題 ^-配列で上記を行う方法がわからない

    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 に答える