zoukankan      html  css  js  c++  java
  • POJ1207The 3n + 1 problem

    1. //这题是很基础的题目!但上级比赛时忽略了一点,总是导致wrong,哎!遗憾!!

    2. #include<iostream>
    3. using namespace std;
    4. #define MAX 10000 int main()
    5. {
    6.  int cy[MAX];
    7.  int num1,num2;  cy[0]=0;  cy[1]=1;
    8.  for(int i=2;i<MAX;i++)
    9.  {  
    10.  int c=1;   int n=i;  
    11.  while(true)   
    12. {    
    13. if(n<i)  
    14.   {   
    15.   c--;   
    16.   c+=cy[n];
    17.     cy[i]=c;
    18.     break;  
    19.   }    
    20. else   
    21.  {    
    22.  if(n%2==0)   
    23.    n=n/2;   
    24.   else   
    25.    n=3*n+1;   
    26.   c++;   
    27.  }  
    28.  }  
    29. }
    30.  while(cin>>num1>>num2)
    31.  {   
    32. int max=0;   if(num1<num2)
    33.   {  
    34.   for(int i=num1;i<=num2;i++)
    35.    {    
    36.  if(cy[i]>max)      max=cy[i];  
    37.   }  
    38.  }   
    39. else
    40.   {  
    41.   for(int i=num2;i<=num1;i++)  
    42.   {     
    43. if(cy[i]>max)      max=cy[i];   
    44.  }    
    45.   }  
    46.  cout<<num1<<" "<<num2<<" "<<max<<endl;
    47.  }  
    48. return 0;
    49. }
  • 相关阅读:
    汉语-词语:理解
    汉语-词语:头脑
    SELECT INTO
    SELECT
    scp
    samba
    rpmgraph
    rpmdeps
    rpmcache
    rpmbuild
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/2858222.html
Copyright © 2011-2022 走看看