V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
spencerqiu
V2EX  ›  问与答

约瑟夫环,求帮忙看程序错在哪里

  •  
  •   spencerqiu · 2014-09-09 21:07:26 +08:00 via iPad · 1938 次点击
    这是一个创建于 3531 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我的思路是:
    1.设定一个布尔数组,所有值赋为真
    2.设定三个累加器 s、j、b ,s 用来报数,j用来循环、联系头和尾,b用来统计圈内剩余人数
    3.出圈的赋为假,输出圈中为真的元素。

    代码如下,是按照思路写的,但是有问题,不曾能找的出来。

    #include<iostream>
    #include<cstdlib>
    using namespace std;
    int a[100];
    int main()
    {
    int m,n;
    cin >>m>>n;
    int b;
    int s=1;
    int i;
    int j=1;
    b=m;
    for(i=1;i<=m;i++)
    a[i]=1;
    do {
    if (a[j]==1)
    s++;
    if (s==n)
    {
    a[i]=0;
    s=1;
    b--;
    }
    j++;
    if (j=n+1)
    j=1;
    }while(b==1);
    for (i=1;i<=m;i++)
    if (a[i]==1)
    {
    cout <<i;
    break;
    }
    system("pause");
    return 0;
    }
    6 条回复    2014-09-10 03:26:56 +08:00
    sophymax
        1
    sophymax  
       2014-09-09 21:17:27 +08:00
    占前排,坐等楼主被喷
    paulw54jrn
        2
    paulw54jrn  
       2014-09-09 21:20:30 +08:00
    好歹发个gist..
    Cee
        3
    Cee  
       2014-09-09 21:24:19 +08:00
    代碼好亂
    中間a[i]=0; 和i有啥關係?
    而且判斷時候你的s已經做了s++了,也是要找到下一個人啊
    Cee
        4
    Cee  
       2014-09-09 21:27:10 +08:00
    做算法題目前先整理一下思路。
    msg7086
        5
    msg7086  
       2014-09-10 03:09:04 +08:00
    首先,你需要把变量的名字弄正常;
    msg7086
        6
    msg7086  
       2014-09-10 03:26:56 +08:00
    随手打了一个链表数组版,轻喷。

    https://gist.github.com/msg7086/03a1eafe4c13566236bc
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1566 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 17:04 · PVG 01:04 · LAX 10:04 · JFK 13:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.