约瑟夫环问题

来源:互联网 发布:如何运行php文件 编辑:程序博客网 时间:2024/06/07 11:38
int joseph(int a[],int n,int m)
{
    for(int i = 0;i<n;i++)
        a[i] = 1;
    int sum = n;
    int start = 0;
    int count = 0;
    while(sum>=2)
    {
        count = 0;
        while(count !=m)
        {
            if(a[start]!=0)
            {
                count++;
                if(count == m)
                    break;


            }
            start = (start + 1)%n;
        }
        a[start] = 0;
        sum--;
        cout<<"the content of the array:"<<endl;
        for(int t = 0;t<n;t++)
            cout<<a[t]<<"\t";
        cout<<endl;
    }
    for(int j = 0;j<n;j++)
        if(a[j]!=0)
        {
            return j+1;
        }


}
原创粉丝点击