校賽3

題意,給定一個函數,由y求x,化爲二進制求就行了
#include<iostream>//根據x求y那道題,A了,題很無語,但化爲二進制串還是要經常搞得,
//還有有道題字符一個一個讀入時T了 ,一行一行讀A了,我不曉得爲什麼,但總之以後要注意,別被這個給卡了 
#include<cstring>
using namespace std;
__int64 y,ans;
__int64 temp;
bool a[50],b[50],c[50];
void solve(__int64 x)
{
   for(int i=0;i<=45;i++)
   {
   if(x%2)
   a[i]=1;
   else 
   a[i]=0;
   x=x/2;
   }
}
int main()
{
	while(scanf("%I64d",&y)!=EOF)
	{
		ans=0;
		if(y==1)
		{
		cout<<"1"<<endl;
	    continue;
		}
		if(y==0)
		{
		cout<<"0"<<endl;
		continue;
		}
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		memset(c,0,sizeof(c));
		   solve(y);
		   for(int i=45;i>0;i--)
		   {
		   	if(a[i])
		   	{
		   	temp=i;
		   	break;
		    }
		   }
		   b[temp]=1;
		   c[temp]=0;
		   c[temp-1]=1;
		   
		for(int j=temp-1;j>=0;j--)
		{
			if(a[j])
			{
			b[j]=!c[j];
			c[j-1]=!c[j];
			}
			else
			{
				b[j]=c[j];
				c[j-1]=c[j];
			}
		}
	
		
		for(int j=0;j<=45;j++)
		{
			//cout<<b[j];
			ans+=b[j]*(1<<j);
		}
		cout<<ans<<endl;
		}
	return 0;	
	}
	

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章