| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
怪事,用那个?:来求转换大小为什么给了个wa,而用if去ac呢,请高手指导一下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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator