------------------ 原始邮件 ------------------
发送时间: 2011年7月1日(星期五) 晚上10:58
主题: Re: 3n+1
Liu Ming,
Have your code passed the UVA online judge? Something may not work, e.g., #include "stdafx.h", "_tmain".
Regards,
Henry
2011/6/29 LiuMing
<wooo...@gmail.com>
I have finished the first one, here is the code:
// 3n+1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<math.h>
#include<stdlib.h>
void swap(int a, int b);
int cycle_length(int n);
int _tmain(int argc, _TCHAR* argv[])
{
int par1,par2;
par1=par2=0;
printf("Input: \n");
scanf("%d %d",&par1,&par2);
//printf("%d %d\n",par1,par2);
if(par1 > par2)
{
swap(par1, par2);
}
int i=par1;
int MaxLen = 0;
MaxLen = cycle_length(par1);
while(i <= par2)
{
//printf("cycle_length(%d)=%d\n",i,cycle_length(i));
if(MaxLen <= cy5cle_length(i))
MaxLen=cycle_length(i);
//printf("MaxLen=%d\n",MaxLen);
i++;
}
printf("Output: \n");
printf("%d %d %d\n",par1,par2,MaxLen);
return 0;
}
void swap(int a, int b)
{
int temp = a;
a= b;
b =temp;
}
int cycle_length(int n)
{
int count = 1;
while(n != 1)
{
count++;
if( n % 2 == 0)
{
n /=2;
continue;
}
else
{
n = 3 * n + 1;
continue;
}
}
return count;
}
--
Hong-Ning Dai, Ph.D.
Assistant Professor
Faculty of Information Technology
Macau University of Science and Technology
Tel: (853) 8897 2154
Email:
hndai...@gmail.com