POJ1023 The Fun Number System

#include<iostream>
#include 
<string>
using namespace std;

string str;;
__int64 N;
int k;
int result[100];
int index = 0;

int main()
{
    
int t,i;
    cin
>>t;
    
while(t--)
    {
        cin 
>> k;
        cin 
>> str;
        cin 
>> N;
        
for(i = k-1;i >= 0--i)
        {
            
if((N%2==1|| (N%2 == -1))
            {
//奇数
                
                
if(str[i] == 'p')
                {
//减1
                    N-=1;
                    N
/=2;
                }
                
else if (str[i] == 'n')
                {
//加1
                    N+=1;
                    N
/=2;
                }
                result[index
++= 1;
            }
            
else
            {
//偶数
                N/=2;
                result[index
++= 0;
            }
        }
        
if(N != 0)
        {
            cout
<<"Impossible";
        }
        
else
        {
            
for(i = k-1; i >= 0--i)
            {
                cout 
<< result[i];
            }
        }
        cout 
<< endl;
        index 
= 0;
    }
    
return 0;
}

posted on 2009-09-13 10:15  Phinecos(洞庭散人)  阅读(1054)  评论(0编辑  收藏  举报

导航