[ACM ] 100 3n+1 problem 如何再改善?
小弟練了一年
終於把ACM 最簡單的3n+1 problem寫出來
顯示AC了
不過爬了一下文
這題好像有好多種解
想請問我這樣寫 有沒有什麼不妥的地方呢?
而且花了1.26秒....好久@@
=======================================================
#include<stdio.h>
#include<stdlib.h>
int length(int n);
int main()
{
int a=0,b=0,temp=0,max,i,index;
while( (scanf("%d %d",&a,&b))!=EOF )
{
printf("%d %d ",a,b);
if(a>b)
{
temp=a;
a=b;
b=temp;
}
max=length(a);
for(i=a;i<=b;i++)
{
if(length(i)>max)
{
max=length(i);
index=i;
}
} //for
printf("%d\n",max);
} //while
system("pause");
return 0;
}
int length(int n) //傳回整數n的cycle-length值
{
int count=1;
while(1)
{
if(n==1)
{
return count;
}
else if(n%2==1)
{
count++;
n=3*n+1;
continue;
}
else
{
count++;
n=n/2;
continue;
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.91.19
推
03/20 01:34, , 1F
03/20 01:34, 1F
→
03/20 01:39, , 2F
03/20 01:39, 2F
推
03/20 02:17, , 3F
03/20 02:17, 3F
→
03/20 06:26, , 4F
03/20 06:26, 4F
討論串 (同標題文章)