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

怪事,用那个?:来求转换大小为什么给了个wa,而用if去ac呢,请高手指导一下

Posted by xisheng at 2008-10-22 06:41:01 on Problem 1207
wa的代码:
#include<iostream>
using namespace std;

long numbers(int x)
{
	long num=1;
	
	
 	while(1){

	if(x%2==0)
		x=x/2;
	else
		x=3*x+1;
    
	num++;
	if(x==1) break;
	}
		return num;

}
 int main()
 {
	 int i,j,c,d,e; 

	long a,b;

	 while(cin>>i){
		  cin>>j;
	
			 d=i<j?i:j;
			 e=i<j?j:i;
		

		
		
		
		 a=numbers(i);
		 for(c=d;c<=e;c++){
			 b=numbers(c);
			 if(b>a)a=b;
		 }
		 
	 cout<<i<<" "<<j<<" "<<a<<endl;

	 }
	 return 0;
 }

	 

ac的代码:
#include<iostream>
using namespace std;

long numbers(int x)
{
	long num=1;	
 	while(x!=1){
	if(x%2==0)
		x=x/2;
	else
		x=3*x+1;
	num++;
	}
		return num;
}
 int main()
 {
	int i,j,ii,jj; 
	 long a,b,c;

	 while(cin>>i>>j){
		 if(i>j){
			 ii = j;
			 jj = i;
		 }else{
			 ii = i;
			 jj = j;
		 }
		 a=numbers(ii);
		 for(c=ii;c<=jj;c++){
			 b=numbers(c);
			 if(b>a)a=b;
		 }
	 cout<<i<<" "<<j<<" "<<a<<endl;
	 }
	 return 0;
 }

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