• 【codeforces 777A】Shell Game


    【题目链接】:http://codeforces.com/contest/777/problem/A

    【题意】
    奇数次操作交换1,2位置的东西;
    偶数此操作交换2,3位置的东西
    给你操作的次数,和最后的位置;
    问你一开始的位置在哪里.

    【题解】

    /*
        一开始在0位置
        1 2 3 4 5 6 
        1 2 2 1 0 0 
        这就是一个循环,到了第6次操作又回到了初始状态;
        n%6 == 1 - > 1
            == 2 ->2
            ...
            == 0 ->0
        ->最终的位置就和n%6的值有关;
        在1位置
        1 2 3 4 5 6
        0 0 1 2 2 1
        第6次操作就回到了初始状态;
    
        在2位置
        1 2 3 4 5 6
        2 1 0 0 1 2
        第6次操作回到初始状态
    
        枚举一开始的位置就好;看看哪个和答案符合;
    */


    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%lld",&x)
    
    typedef pair<int, int> pii;
    typedef pair<LL, LL> pll;
    
    const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
    const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
    const double pi = acos(-1.0);
    const int N = 110;
    
    int zero[7] = { -1,1 ,2, 2, 1 ,0 ,0 };
    int one[7] = { -1,0, 0, 1, 2, 2, 1 };
    int two[7] = { -1,2, 1, 0, 0, 1, 2 };
    
    LL n;
    int x;
    
    int main()
    {
        //freopen("F:\rush.txt", "r", stdin);
        rel(n);
        rei(x);
        LL temp = n % 6;
        if (temp == 0)
            temp = 6;
        if (zero[temp] == x)
        {
            puts("0");
            return 0;
        }
        if (one[temp] == x)
        {
            puts("1");
            return 0;
        }
        if (two[temp] == x)
        {
            puts("2");
            return 0;
        }
        return 0;
    }
  • 相关阅读:
    【Redis】跳跃表原理分析与基本代码实现(java)
    小鹤音形指引
    Maven
    算法思维(长期更)
    多路平衡树之红黑树
    多路平衡树之B树
    多路平衡树之2-3查找树
    栈与队列
    树基本概念
    Vue学习
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626596.html
Copyright © 2020-2023  润新知