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

基本的回溯题,咋这么少的人AC了呢。。贴AC代码

Posted by yzhw at 2009-06-22 08:39:35 on Problem 1078
Source Code

Problem: 1078  User: yzhw 
Memory: 5728K  Time: 313MS 
Language: Java  Result: Accepted 

Source Code 
import java.util.*;
import java.io.*;
public class Main {

	static boolean used[]=new boolean[101];
	static boolean flag,find;
	static boolean chk(int pos,int num1,int num2)
	{
		if(num1==1)
		{
			if(flag) return true;
			else
			{
				flag=find=true;
				if(chk(1,num2,num2)) return true;
				flag=false;
				return false;
			}
			
		}
		else if(pos>100) return false;
		else
		{
			for(int i=pos;i<=100;i++)
			{
				if(num1%i==0&&!used[i]) 
					{
				      used[i]=true;
					  if(chk(pos+1,num1/i,num2)) return true; 
					  used[i]=false;
					}
			}
			return false;
		}
			
	}
	public static void main(String[] args) throws Exception{
		Scanner in=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int a,b;
		while(in.hasNextInt())
		{
			a=in.nextInt();
			b=in.nextInt();
			if(a<b)
			{
				a=a+b;
				b=a-b;
				a=a-b;
			}
			flag=false;
			find=false;
			Arrays.fill(used, false);
			boolean res=chk(1,b,a);
			if(res) System.out.println(a);
			else if(find) System.out.println(b);
			else System.out.println(a);
			
		}

	}

}


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