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

1023 用回溯法,这样写代归函数,为什么不行呢?帮忙!

Posted by GaryLiang at 2009-08-03 13:02:24
#include <iostream>
#include <string.h>
#include <vector>
#include <cstring>
#include <stdio.h>
#include <cmath>
using namespace std;
int total=0;
static bool gflag=true;
static bool fg=false;
void Result(int bits,int rt,int order,long double temp,char * np,int * flag,vector<bool> & can){
	
	while(::gflag){
		
		if(rt==(int)temp){
			for(int i=order;i<bits;i++){
				flag[i]=0;
			}
			can.push_back(true);
			fg=true;
			gflag=false;
			
			return;
		}

		if(order>=bits){
			return;
		}
		
		int max=(int)pow((long double)2,bits);
		if(total>max){
			can.push_back(false);
			gflag=false;
			return;
		}
		char tp=np[bits-1-order];
		if(!(::fg)){
			flag[order]=1;
			

			if(tp=='p'){
				temp=temp+pow((long double)2,order);
			}
			else{
				temp=temp-pow((long double)2,order);
			}

			Result(bits,rt,order+1,temp,np,flag,can);
		}
		if(!(::fg)){
			flag[order]=0;
			if(tp=='p'){
				temp=temp-pow((long double)2,order);
			}
			else{
				temp=temp+pow((long double)2,order);
			}

			Result(bits,rt,order+1,temp,np,flag,can);
		}
	}
}

int main(){
	int count;

	vector<bool> can;
	vector<string> result;
	cin>>count;
	while(count>0){
		int bits;
		int rt;
		
		cin>>bits;

		char * np=(char *)new char[bits+1];
		int * flag=(int *)new int[bits+1];
		for(int i=0;i<bits;i++){
			flag[i]=0;
		}
		scanf("%s",np);
		cin>>rt;
		int order=0;
		::total=0;
		::gflag=true;
		::fg=false;
		Result(bits,rt,order,0,np,flag,can);
		for(int i=bits-1;i>=0;i--){
			cout<<flag[i];
		}

	}

}

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