V - The Fun Number System(POJ 1023)

Description

In a k bit 2’s complement number, where the bits are indexed from 0 to k-1, the weight of the most significant bit (i.e., in position k-1), is -2^(k-1), and the weight of a bit in any position i (0 ≤ i < k-1) is 2^i. For example, a 3 bit number 101 is -2^2 + 0 + 2^0 = -3. A negatively weighted bit is called a negabit (such as the most significant bit in a 2’s complement number), and a positively weighted bit is called a posibit.
A Fun number system is a positional binary number system, where each bit can be either a negabit, or a posibit. For example consider a 3-bit fun number system Fun3, where bits in positions 0, and 2 are posibits, and the bit in position 1 is a negabit. (110)Fun3 is evaluated as 22-21 + 0 = 3. Now you are going to have fun with the Fun number systems! You are given the description of a k-bit Fun number system Funk, and an integer N (possibly negative. You should determine the k bits of a representation of N in Funk, or report that it is not possible to represent the given N in the given Funk. For example, a representation of -1 in the Fun3 number system (defined above), is 011 (evaluated as 0 - 2^1 + 2^0), and
representing 6 in Fun3 is impossible.

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by the input data for each test case. Each test case is given in three consecutive lines. In the first line there is a positive integer k (1 ≤ k ≤ 64). In the second line of a test data there is a string of length k, composed only of letters n, and p, describing the Fun number system for that test data, where each n § indicates that the bit in that position is a negabit (posibit).
The third line of each test data contains an integer N (-2^63 ≤ N < 2^63), the number to be represented in the Funk number
system by your program.

Output

For each test data, you should print one line containing either a k-bit string representing the given number N in the Funk number system, or the word Impossible, when it is impossible to represent the given number.

Sample Input
2
3
pnp
6
4
ppnn
10
Sample Output
Impossible
1110
题解

题意:

  • 给一个n,p序列,代表二进制序列的权值,n为负,p为正。例如,111用序列ppn赋权表示的十进制数为1 * 22 + 1 * 21+ 1 * (-1) * 20
  • 给定一个np序列及其长度,和一个十进制数,求该数的赋权二进制数,若不能表示,则输出Impossible

思路:

  • 对于一个十进制整数,若不考虑赋权,将其转化为二进制序列,可以由低位到高位考虑,当前为偶数,末位记为0,奇数记为1,将这个数右移一位,重复上面操作。
  • 如果考虑赋权,与上面不同的是对奇数的处理,如果该位权值为正,则末位为1,右移一位;如果为负,则末位记为1,右移一位,再加一(抵消负权值的1)。
Code
#include<iostream>
using namespace std;

int main()
{
    int t, k; 
    long long n;
    char sign[66];
    int bit[66] = {0};
    cin>>t;
    while (t--)
    {
        cin>>k;
        for(int i = 0; i < k; i++)
            cin>>sign[k-1 - i];

        cin>>n; 

        for(int i = 0; i < k; i++)
        {
            if(n & 1)//n当前为奇数
            { 
                bit[i] = 1;//奇数,赋值为1
                if(sign[i] == 'p')  n >>= 1;//此位权值为正,右移一位
                else                n = (n>>1) + 1;//此为权值位负,右移一位,再加1
                                                  //是因为,负权值使之减一,与原值差二,所以在高一位加1
            } 
            else //n为偶数
            {
                bit[i] = 0;//偶数直接赋值0,右移一位
                n >>= 1;
            }
        }

        if(n != 0) //如果移动k位,用尽最大表示位数,n还不为0,说明无法表示n
        { 
            cout<<"Impossible"<<endl; 
            continue;
        }

        for(int i = k; i > 0; i--)
        {
            if(bit[i-1])    cout<<1;
            else           cout<<0;
            
        }
        cout<<endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值