HackerRank

Intuitive coding problem. But several details to take care.

#include <cmath>
#include <cstdio>
#include <vector>
#include <bitset>
#include <iostream>
#include <algorithm>
#include <unordered_set>
using namespace std;

bool play(unsigned long long v)
{
    std::bitset<64> b(v);

    int lastOnBit = 63;
    bool bRet = true;
    while (v != 1)
    {
        if (b.count() == 1)
        {
            v >>= 1;
        }
        else
        {
            int i;
            for (i = lastOnBit; i >= 0; i--)
            {
                if (b[i]) break;
            }
            lastOnBit = i;
            v <<= (64 - i);
            v >>= (64 - i);
        }
        b = v;
        bRet = !bRet;
    }

    return bRet;
}

int main() 
{
    int t; cin >> t;
    while (t--)
    {
        unsigned long long v; cin >> v;
        cout << (play(v) ? "Richard" : "Louise") << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/tonix/p/4440575.html