zoukankan      html  css  js  c++  java
  • 5.约瑟夫问题[数组法]

    另一种解法:将数组全部置1,累计相加到报数,则将该位置置为0,意为该位出列,如此反复。关键在于:把数组作环状处理,这个手法已经演练很多遍了!

    /*------------完整代码@映雪-------------*/
    
    #include <iostream>
    using namespace std;
    int main()
    {
        int count=0,s=0,i,j=-1,n=3,m=3;
        int a[100];
        for(i=0;i<n;i++)/*将数组全部置1*/
        a[i]=1;
        while(count<n)
        {
            j=(j+1)%n;
            s=s+a[j];
            if(s==m)/*累加到报数*/
            {
                a[j]=0;/*位置置0,表示出列*/
                s=0;
                count++;/*出列计数+1*/
                printf("%d ",j+1);
            }
        }
        return 0;
    }
  • 相关阅读:
    Codeforces_739_B
    Codeforces_732_D
    D
    C
    E
    商汤AI园区的n个路口(中等)
    D. The Fair Nut and the Best Path
    HDU6446
    分解质因数(线性筛)
    D. Extra Element
  • 原文地址:https://www.cnblogs.com/tinaluo/p/5274563.html
Copyright © 2011-2022 走看看