码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1023 The Fun Number System

时间:2017-12-10 22:47:29      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:follow   repo   har   一个   put   sys   ios   port   ssi   

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

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest
 
    题目大致意思:恩......给你一个串,只由n和p组成,再给你一个数字,让你求一个二进制序列使得这个二进制序列按照那个串的规则变换后得到给定的数字。
    其中如果碰到p,那就是 二进制序列的第i个数 * 2^i   否则就是 二进制数序列的第i个数 * -2^i
    分析:
    1.我也不知道怎么做,看别人的......
    2.不过我知道一点:排除二进制数最后一个数不看,前面的二进制数按照串的规则最后得到的必然是偶数,如果最后一个是二进制数是0,那么肯定是偶数了,否则就是奇数;也就是说,如果给定的数字是偶数或者奇数,我们至少能够100%确定最后一个二进制数是0还是1
    3.至于后面的折半...移位...我是真的迷,等那天弄明白了再来更新吧
 
代码如下:
#include <iostream>

using namespace std;

int main()
{
    int test;
    __int64 len, goal;
    char str[1000];
    char ans[1000];
    cin >> test;
    while (test--)
    {
        cin >> len >> str >> goal;
        ans[len] = \0;
        for (int i = len - 1; i >= 0; i--)
        {
            if (goal % 2 == 1 || goal % 2 == -1)
            {
                ans[i] = 1;
                if (str[i] == p) goal = (goal - 1) / 2;
                else goal = (goal + 1) / 2;
            }
            else
            {
                ans[i] = 0;
                goal /= 2;
            }
        }
        if (!goal) cout << ans << endl;
        else cout << "Impossible" << endl;
    }
    return 0;
}

 

POJ 1023 The Fun Number System

标签:follow   repo   har   一个   put   sys   ios   port   ssi   

原文地址:http://www.cnblogs.com/LHJL8023/p/8018202.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!