Description
We define function F like this:
if(x==1)
F[x]=1;
else if(x%2==1)
F[x]=2*F[x/2]+1;
else
F[x]=2*F[x/2]-1.
Of course it is easy for us to write code to calculate F[x].But my problem is a little more difficult, I will give you x in binary form, and you must give me F[x] in binary form too.
Input Description
The input contains T test cases, intdicting in first line.
For each case,you are given x in binary form, it is assumed that the length of x will not longer than 10000.
For each case,you are given x in binary form, it is assumed that the length of x will not longer than 10000.
Output Description
For each case,output F(n) in binary form.
Sample Input
2 1 10
Sample Output
1 1
尼玛啊。我当时用递归写啊。RE了一早上啊。。。。。
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
string a;
int t;
scanf("%d",&t);
while(t--)
{
cin>>a;
int len=a.size();
string b="1";
for(int i=1;i<len;i++)
{
if(a[i]=='1')
{
b=b+"1";
}
else
{
b=b+"0";
int len1=b.size();
int j=len1-1;
while(b[j]=='0')
{
b[j]='1';
if(j==0)break;
j--;
}
b[j]='0';
if(b[0]=='0')
{
string c;
c.resize(b.size()-1);
for(int k=0;k<len1-1;k++)
{
c[k]=b[k+1];
}
b=c;
}
}
}
cout<<b<<endl;
}
return 0;
}