回复: 3n+1

0 views
Skip to first unread message

windowsx7771

unread,
Jul 1, 2011, 11:09:42 AM7/1/11
to acm-cpc-must
yes, it will occur a compiling error, but only need replace the #include "stdafx.h" with #include<stdio.h>, and the int _tmain(int argc, _TCHAR* argv[]) should changed to be int main(int argc, char* argv[]). This algorithm is not enough fast, and we discuss together, then decide use the algorithm that showed in the ChengYiCong's code. This is algorithm can work with the test 1 999999.
 
 
------------------ 原始邮件 ------------------
发件人: "Henry Hong-Ning Dai"<hndai...@gmail.com>;
发送时间: 2011年7月1日(星期五) 晚上10:58
收件人: "acm-cpc-must"<acm-cp...@googlegroups.com>;
主题: 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
Reply all
Reply to author
Forward
0 new messages