czxr2436
Mr.BaSO4
2020-10-01 11:58
采纳率: 100%
浏览 77

做约瑟夫环问题时,程序计算不出来,想请前辈们看看哪里有问题?

图片说明

运行长这样,代码如下

//约瑟夫环
#include
using namespace std;
//让所有的猴子为0,淘汰的加1
int Nextone(int i, int total)
{
if (i < total - 1) i++;
else i = 0;
return i;
}//角标i从0到长度减一遍历,初始值为0
int Nextsaid(int said, int outnum)
{
if (said < outnum)
said++;
if (said = outnum)
said = 1;
return said;
}//让said从1到outnum遍历,
int winner(int total, int outnum)
{
int* a = new int[total];//创建一个长度等于猴子总数的数组
for (int k = 0; k < total; k++)
{
a[k] = 0;
}//让所有的猴子为0
int now = total, said = outnum, i = 0;//now表示剩余猴子的数目,said表示猴子所报的数字,i是角标
while (now > 1)//当剩余的猴子数目大于1时
{
if (a[i] = 0)
{
said = Nextsaid(said, outnum);
cout << "第" << i + 1 << "只猴子报" << said;
if (said = outnum)
{
a[i] = 1;//被淘汰的猴子为1,不再计数
now = now - 1;//剩余的猴子数减一
cout << "它被淘汰了!";
}
cout << endl;
}
i = Nextone(i, total);
}
return i+1;
}
int main()
{
int total, outnum;
cout << "请输入猴子的总数:"; cin >> total;
cout << "请输入淘汰者所报的数字:"; cin >> outnum;
cout << "计算中" << endl;
cout << "胜利者是第" << winner(total, outnum) << "号猴子!" << endl;
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • caozhy
    已采纳

    if (a[i] = 0)
    ->
    if (a[i] == 0)

    还有
    if (said = outnum)
    ->
    if (said == outnum)

    点赞 评论

相关推荐