Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

把所有的负数N转化为正数-N,可以直接用__int64解决!

Posted by nudt_zhangkai at 2007-02-06 00:33:38 on Problem 1023
如果N可以被表示,-N也一定可以被表示,此时只需要把n换成p,p换成n即可。
#include "iostream.h"
int main()
{
	int i,n,K;
	int a[100]={0},b[100]={0};
	char ch;
	cin>>n;
	__int64 N,MAX;
	while(n--)
	{
		cin>>K;
		for(i=0;i<K;i++)
		{
			cin>>ch;
			if(ch=='p')
				a[i]=1;
			else
				a[i]=-1;
			b[i]=0;
		}
		N=0;MAX=0;
		cin>>ch;
		if(ch=='-')
		{
			for(i=0;i<K;i++)
				a[i]=-a[i];
		}
		else
			N+=(ch-'0');
		while((ch=cin.get())>='0'&&ch<='9')
			N=N*10+(ch-'0');
		
		for(i=0;i<K;i++)
		{
			MAX=(a[i]>0)?((MAX<<1)+1):(MAX<<1);
		}
		if(N>MAX)
		{
			cout<<"Impossible"<<endl;
			continue;
		}

		for(i=K-1;i>=0;i--)
		{
			if(N%2==1)
			{
				b[i]=1;
				N=((N-a[i])>>1);
			}
			else
			{
				b[i]=0;
				N=N>>1;
			}
		}
		if(N==0)
		{
			for(i=0;i<K;i++)
				cout<<b[i];
			cout<<endl;			
		}
		else
		{
			cout<<"Impossible"<<endl;
			
		}
	}

	return 1;
	
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator