zoukankan      html  css  js  c++  java
  • 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

    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] = '';
            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;
    }

    转载于:https://www.cnblogs.com/LHJL8023/p/8018202.html

  • 相关阅读:
    SCILAB简介[z]
    UG OPEN API编程基础 2约定及编程初步
    Office 2003与Office 2010不能共存的解决方案
    UG OPEN API 编程基础 3用户界面接口
    NewtonRaphson method
    UG OPEN API编程基础 13MenuScript应用
    UG OPEN API编程基础 14API、UIStyler及MenuScript联合开发
    UG OPEN API编程基础 4部件文件的相关操作
    UG OPEN API编程基础 1概述
    16 UG Open的MFC应用
  • 原文地址:https://www.cnblogs.com/twodog/p/12137989.html
Copyright © 2011-2022 走看看