Codeforces Perfect Pair (JAVA)

來源:互聯網
上載者:User

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);  }}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.