SPOKES WHEEL

Mr. Miyamoto is playing with a wheel. Each wheel can have many spokes. A spoke is one of the rods radiating from the center of a wheel (the hub where the axle connects), connecting the hub with the round traction surface. The wheel has 32 spokes with random of 0 or 1 as the value/tag for each spoke.

He has given a list of spokes, before and after its movement. His task is to calculate its optimal movement, whether the spokes move (in rotation) to the left or right. The list of spokes can be represented in a hexadecimal. For example: 800000001, means:

       1000 0000 0000 0000 0000 0000 0000 0000 (list of spokes before its movement) and

       0000 0000 0000 0000 0000 0000 0000 0001 (after its movement)

From this example, he found that there are two possible rotations: move to the left 1 (one) time and move to the right 31 times. The optimal rotation is the minimal number to move (in rotation), which is move to the left 1 (one) time.

Input

The first line of input gives the number of test cases, T (1 ≤ T ≤ 1000), followed by a line which content of the N1 as a state before the spokes move and N2 as a state after the spokes move. Whereas N1, N2 are 32-bits integer in hexadecimal representation (1,2,3,...,9,0,A,...,E,F)

Output

For each case, output in a line "Case #X: Y" (without quotes) where X is the case number starting from 1, and Y is the minimum number of wheel's spoke movements and followed by the direction to the "Left" or "Right". If the number of movements to the left or right is the same, the direction will be written as "Any". If N2 is not the final state of N1 then the output will be written as "Not possible".

Sample Input

4
80000000 1
1 80000000
AAAAAAAA 55555555
1 7

Sample Output

Case #1: 1 Left
Case #2: 1 Right
Case #3: 1 Any
Case #4: Not possible

题意

两个16进制数N1, N2,换成32位2进制数,N1可以进行左移或者右移

问:N1 最少多少步能得到N2

这道是一个模拟题,不过据说用位运算简单很多;


右移同理  也就是    a=(a>>31)|(a<<1) 达到了模拟左移的效果  a=(a<<31)|(a>>1) 达到了模拟右移的效果

ps:输入时可直接用%x(以16进制的方式读入一个数)

下面直接贴代码

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    int n;
    unsigned int a,b;
    unsigned int x,y;
    scanf("%d",&n);
    int i=0;
    while(n--)
    {
        i++;
        scanf("%x%x",&a,&b);
        int left=0,right=0;
        x=a;
        y=b;
        while(x!=y&&left<32)
        {
            x=(x>>31)|(x<<1);
            left++;
        }
        x=a;
        while(x!=y&&right<32)
        {
            x=(x<<31)|(x>>1);
            right++;
        }
        if (left == 32)
                 printf("Case #%d: Not possible\n", i);
		else if (left == right)
		         printf("Case #%d: %d Any\n", i, left);
		else if (left<right)
                 printf("Case #%d: %d Left\n", i, left);
		else
		   printf("Case #%d: %d Right\n", i, right);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值