#2019120500011-LG 约瑟夫问题&玩具谜题

P1996 P1563 模拟

P1996 约瑟夫问题

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main( ){
    int n;
    scanf("%d",&n);
    int m;
    scanf("%d",&m);
    bool vis[n+2];  
    for(int j=1;j<=n;j++){
        vis[j]=1;
    }
    //memset(vis,0,sizeof(vis))
    int now=0,i=1,g=n;
    while(g>0){
        if(i>n) i-=n;
        if(vis[i]==1){
            now++;
            if(now==m){
                vis[i]=0;
                printf("%d ",i);
                g--;
                now=0;
            } 
        }//有人 
        i++;
    }
    return 0;
} 

P1563 玩具谜题

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#define N 100005
using namespace std;
struct person{
    bool a;//0向内 1向外 
    char s[12];
}pep[N];
long long x,y;
//way ansdirct;
int main( ){
    long long n,m;
    scanf("%lld%lld",&n,&m);//wanju geshu he zhiling tiaoshu
    for(int i=0;i<n;i++){
        //scanf("%d%s",&pep[i].a,pep[i].s);
        cin>>pep[i].a>>pep[i].s;
    }
    int now=0;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        if(pep[now].a==0&&x==0)now=(now+n-y)%n;
        else if(pep[now].a==0&&x==1)now=(now+y)%n;
        else if(pep[now].a==1&&x==0)now=(now+y)%n;
        else if(pep[now].a==1&&x==1)now=(now+n-y)%n;
    }
    printf("%s",pep[now].s);
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/liuziwen0224/p/11992382.html