2013年9月3日 星期二

UVA Q100 解題

最近想練習C++  所以找了 http://luckycat.kshs.kh.edu.tw/ 此網站的題目來練習



寫出來的解法應該不完美 僅供參考




Q:100


#include <iostream>

using namespace std;
int algo(int,int);
void swap(int &,int &);

void main()
{
int i,j =NULL;
cout<<"please keyin 0<i ,j<1,000,000"<<endl;
cout<<"i =  ";
cin>>i;cout<<endl;

cout<<"j =  ";
cin>>j;cout<<endl;

int MAX=0;
//check i j , if i> j swap(i,j)
if(i>j)
{
swap(i,j);
}

for(int x=i;x<j;x++)
{
int y=algo(x,0);
if(y>MAX)MAX=y;
}
cout<<"MAX= " << MAX <<endl;
system("pause");
}


int algo(int n,int count)
{
count++;
if (n==1) return count;
else
{
 if (n%2 == 1 )
 n=3*n+1;
 else n=n/2;
 algo(n,count);
}

}

void swap(int &i,int &j){int tmp=j;j=i;i=tmp;}

沒有留言:

張貼留言