C#代码解决约瑟夫问题

From , 3 Years ago, written in C#, viewed 190 times.
URL https://pastebin.vip/view/85203ae8
  1. public class Josephas  
  2.    
  3.  //从第start人开始计数,以alter为单位循环记数出列,总人数为total  
  4.    public int [ ] Jose(int total, int alter, int start)      
  5.    {            
  6.         int i, j, k = 0;  
  7.  
  8.         //count数组存储按出列顺序的数据,以当结果返回  
  9.         int[] count = new int [ total+1];      
  10.  
  11.         //s数组存储初始数据  
  12.         int[] s = new int [ total+1];      
  13.  
  14.         //对数组s赋初值,第一个人序号为0,第二人为1,依此下去  
  15.         for (i = 0; i <total; i++)      
  16.         {  
  17.             s[i] = i;  
  18.        }  
  19.  
  20.         //按出列次序依次存于数组count中  
  21.         for (i = total; i >= 2; i--)        
  22.        {  
  23.             start = (start + alter - 1) % i;  
  24.             if (start == 0)  
  25.                 start = i;  
  26.             count[k] = s[start];  
  27.             k++;  
  28.             for (j = start + 1; j <= i; j++)          
  29.          s[j - 1] = s[j];  
  30.         }                 
  31.         count[k]=s[1];  
  32.  
  33.         //结果返回  
  34.         return count;      
  35.     }  
  36. }  
  37. //csharp/7177

Reply to "C#代码解决约瑟夫问题"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website