Solution to a problem CF777A] [Shell Game

Topic Portal
go first law

  1. 0,1,2
  2. 1,0,2
  3. 1,2,0
  4. 2,1,0
  5. 2,0,1
  6. 0,2,1
  7. 0,1,2

Found cycle lasts 6, it may be n% 6, and then direct simulation

Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,x,i,a[3]={0,1,2};
    scanf("%d%d",&n,&x);//输入
    n%=6;//将n%6
    for(i=1;i<=n;i++)if(i%2==0)swap(a[1],a[2]);else swap(a[0],a[1]);//如果是偶数次将中间和右边交换,否则将中间和左边交换
    printf("%d",a[x]);//输出
    return 0;
}

Guess you like

Origin www.cnblogs.com/pzc2004/p/11600826.html