ACM: 二进制问题 数论题 poj 1023

The Fun Number System
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 2^2-2^1 + 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 (p) 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

题意: 现在给你一个k位数的二进制数, 并且规定每位上的正负号, p为正, n为负, 并且给你一个十进制数n
      判断这种二进制是否可以得到十进制数n, 不能输出Impossible, 否则输出这个二进制数.

解题思路:
        1. 二进制数问题, 因为二进制每位上面不是0就是1, 这个特征很重要.
        2. 重当前最后一位出发观察, 当n为奇数时, 最后一位必须是1, 否则最后一位为0;
           不过本题是有正负之分的, 如果当前最后一位是正数, 则 n = (n-1)/2;
           当当前最后一位是负数时, n = (n+1)/2;
        3. 为什么要说明是当前最后一位呢? 并且是n = (n+/-1)/2呢?
           因为问题是可以递推求解, 每次确定当前最后一位.最后能否得出结果,可以看作当
           k位都确定0或1时, n是否为0;

代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 70

int k;
long long num;
char str[MAX];
int flag[MAX];

int main()
{
//    freopen("input.txt","r",stdin);
    int caseNum;
    scanf("%d",&caseNum);
    while(caseNum--)
    {
        scanf("%d %s %lld",&k, str, &num);
        int index = 0;
        for(int i = k-1; i >= 0; --i)
        {
            if(num % 2 == 1 || num % 2 == -1)
            {
                if(str[i] == 'p')
                {
                    num -= 1;
                    num /= 2;
                }
                else
                {
                    num += 1;
                    num /= 2;
                }
                flag[index++] = 1;
            }
            else
            {
                num /= 2;
                flag[index++] = 0;
            }
        }
       
        if(num != 0)
            printf("Impossible\n");
        else
        {
            for(int i = k-1; i >= 0; --i)
                printf("%d",flag[i]);
            printf("\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值