P1996-约瑟夫问题

 1 #include <bits/stdc++.h>
 2 #define _for(i,a,b) for(int i = (a);i < b;i ++)
 3 typedef long long ll;
 4 using namespace std;
 5 int n,m;
 6 int vis[103];
 7 inline ll read()
 8 {
 9     ll ans = 0;
10     char ch = getchar(), last = ' ';
11     while(!isdigit(ch)) last = ch, ch = getchar();
12     while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
13     if(last == '-') ans = -ans;
14     return ans;
15 }
16 inline void write(ll x)
17 {
18     if(x < 0) x = -x, putchar('-');
19     if(x >= 10) write(x / 10);
20     putchar(x % 10 + '0');
21 }
22 
23 int main()
24 {
25     n = read(),m = read();
26     memset(vis,0,sizeof(vis));
27     int k = 0;
28     int i,t = 0;
29     for(int i = m; k != n; i ++)
30     {
31         if(i > n)
32             i = 1;
33         if(vis[i]==1)
34             continue;
35         else if(vis[i]==0 && t!=0)
36             t --;
37         if(vis[i]==0 && t==0)
38         {
39             write(i);
40             printf(" ");
41             vis[i] = 1;
42             k ++;
43             t = m;
44         }
45     }
46 
47     return 0;
48 }

 

posted @ 2019-08-01 18:22  Asurudo  阅读(158)  评论(0编辑  收藏  举报