Codeforces Perfect Pair (JAVA)

http://codeforces.com/problemset/problem/317/A

题意:给两个数字,可以两数相加去替换其中一个数字。问要做多少次,可以让两个数字钟至少一个 >= 目标数字m,输出次数,不可能的话输出-1

比较简单的题目,用来练习JAVA,代码写得有点,呵呵................

 

import java.util.*;

public class Main{
   
  static long max(long x , long y){
      return x > y ? x : y;
  }
  
  static long solve(long a ,long b ,long c){
      long res = 0;
      while( (a >= c || b >= c) == false){
          long sum = a + b;
          long Max = max(a , b);
          a = sum; b = Max;
          res++;
      }
      return res;
  }
  static long __solve(long a , long b , long c){
      long res = (-b + a - 1) / a;
      long tmp = b + res * a;
      res += solve(a,tmp,c);
      return res;
  }
  
  public static void main(String[] args){
      Scanner cin = new Scanner(System.in);
      long a,b,c,res;
      a = cin.nextLong();
      b = cin.nextLong();
      c = cin.nextLong();
      if(a >= c || b >= c)
          res = 0;
      else if(a <= 0 && b <= 0)
          res = -1;
      else if(a >= 0 && b >= 0){
          res = solve(a,b,c);
      }
      else{
          long tmp;
          if(a < b){
              tmp = a; a = b; b = tmp;
          }
          res = __solve(a,b,c);
      }
      
      System.out.println(res);
  }
}

 

 

 

posted @ 2013-06-18 13:52  Titanium  阅读(420)  评论(0编辑  收藏  举报