Test instructions
Give an integer n to see if it can be represented by a 01 string of K-bits, which is similar to the binary representation of an integer, except that the positive or negative of each representation is given by an input string s.
Analysis:
Understand that the only arbitrary binary representation of integers is determined by an integer pair of arbitrary number to seek the remainder of the only axiom derived from this problem is not difficult, the details of this problem is to pay attention to the negative balance: -5/2=-2,-5%2=-1.
Code:
POJ 1023
//sep9
#include <iostream>
#include <stack>
using namespace std;
int k;
Char s[80];
Long long N;
int main ()
{
int cases;
scanf ("%d", &cases);
while (cases--) {
scanf ("%d%s%lld", &k,s,&n);
Stack<int> ans;
int i;
if (n==0) {
puts ("0");
Continue;
}
for (i=k-1;i>=0;--i) {
if (n%2==0) {
ans.push (0);
n/=2;
Continue;
}
if (n%2==1) {
ans.push (1);
if (s[i]== ' P ') {
n=n/2;
} else{
n=n/2+1;
}
Continue;
}
if (n%2==-1) {
ans.push (1);
if (s[i]== ' P ') {
n=n/2-1;
} else{
n= (n+1)/2;
}} if (! ( i==-1&&n==0))
puts ("impossible");
else{
while (!ans.empty ()) {
printf ("%d", ans.top ());
Ans.pop ();
}
printf ("\ n");}
}
return 0;
}