首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Josephus使用数组?

Josephus使用数组?
EN

Stack Overflow用户
提问于 2014-10-23 04:56:00
回答 1查看 1.8K关注 0票数 0

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

我在想像这样的东西-用户选择数量的人前50 PeopleArray变成PeopleArray50

用户在PeopleArray50中选择元素的值他们必须为这50个元素键入50个值

Death User选择3,因此每三个人死亡,我如何从数组中删除该数字。

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

代码语言:javascript
复制
    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

    }
}    
EN

回答 1

Stack Overflow用户

发布于 2014-10-23 06:42:07

如果我没理解错的话,你想做的事情应该是这样的…

代码语言:javascript
复制
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);
}

或者假设totalPeoplenthPerson = 0;这将从总人员列表中删除第n个人。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26516971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档