F[x]

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.
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值