poj1023 进制

来源:互联网 发布:h3c网络工程师证书知乎 编辑:程序博客网 时间:2024/06/11 17:31

The Fun Number System
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 10489 Accepted: 3569

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

23pnp64ppnn10

Sample Output

Impossible1110

题意:

首先给出样例个数

 后面三行:第一行:多少位   

 第二行:每一位表示的正负,p表示正,n表示负   

 第三行:题目想要得到的数

     

  

求:通过这种方式可以得到题目想要得到的数吗(二进制)




#include<stdio.h>int main(){    int T,k;    char a[70];    long long n;    int ans[70];    scanf("%d",&T);    while(T--)    {        scanf("%d",&k);        getchar();        for(int i=0;i<k;i++)            scanf("%c",&a[i]);        scanf("%lld",&n);        for(int i=k-1;i>=0;i--){            if(a[i]=='p'){                if(n%2){                    n=(n-1)/2;                    ans[i]=1;                }                else{                    n=n/2;                    ans[i]=0;                }            }            else{                if(n%2){                    n=(n+1)/2;                    ans[i]=1;                }                else{                    n=n/2;                    ans[i]=0;                }            }        }        if(n==0){            for(int i=0;i<k;i++)                printf("%d",ans[i]);        }        else            printf("Impossible");        puts("");    }    return 0;}


0 0
原创粉丝点击