约瑟夫环问题直接解法

 1 #include"stdio.h"
 2 int main()
 3 {
 4     int a[10001];
 5     int m,n,temp;
 6     int i;
 7     int count = 1,num = 0;
 8     scanf("%d %d",&n,&m);
 9     for(i=0;i<n;i++)
10         scanf("%d",a+i);
11     while(num != n-1)
12     {
13         for(i=num;i<n;i++)
14         {
15             if(count == m)
16             {
17                 temp = a[num];
18                 a[num] = a[i];
19                 a[i] = temp;
20                 num++;
21                 count = 1;
22             }
23             else
24                 count++;
25             if(i == n-1)
26                 i = num;
27         }
28     }
29     for(i=0;i<n;i++)
30         printf("%d\t",a[i]);
31     return 0;
32 }

 

posted @ 2017-11-02 16:09  __迷途的羔羊  阅读(242)  评论(0编辑  收藏  举报