1207--终于知道内存的宝贵

来源:互联网 发布:淘宝信誉怎么算 编辑:程序博客网 时间:2024/05/19 07:08

刚来了题很简单的题目http://acm.pku.edu.cn/JudgeOnline/problem?id=1207 ,本来用递归立刻AC,不过为了效率(呵呵,借口),我来了个记忆搜索,咋知道临时数组开到int 20000000还不够,终于Memory Limited,呵呵,自找麻烦(其实没分析清楚而已)

import java.util.*;
public class Main {
 //public static int m[] = new int[20000000];
 public static int circleLength(int n){
  //int t = m[n];
  //if(t > 0)return t;
  if(n==1)return 1;//
  int tmp = 0;
  if(n%2 != 0){
   tmp = 3 * n + 1;
  }
  else
   tmp = n / 2;
  return 1 + circleLength(tmp);
 }
 /**
  * @param args
  */
 public static void main(String[] args) {
  //m[1] = 1;
  int t1 = 0;
  int t2 = 0;
  Scanner scan = new Scanner(System.in);
  while(scan.hasNextInt()){
   int ot1 = scan.nextInt();
   int ot2 = scan.nextInt();
   int t = ot1;
   if(ot2<ot1){
    t1 = ot2;
    t2 = t;
   }
   else{
    t1 = ot1;
    t2 = ot2;
   }
   int max = 1;
   for(int i=t1;i<=t2;i++){
    int tmp = circleLength(i);
    if(tmp > max)max=tmp;
   }
   System.out.println(ot1+" "+ot2+" "+max);
  }
 }
 
}

原创粉丝点击