1207--終於知道內存的寶貴

剛來了題很簡單的題目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);
  }
 }
 
}

發佈了30 篇原創文章 · 獲贊 1 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章