|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Seawolf 于 2019-8-13 11:38 编辑
继续 level 3
因为number的大小 could be 10^50, so we still need big integer.
- import java.math.BigInteger;
- public class main {
- public static void main(String[] args){
- String x = "4";
- String y = "11";
- BigInteger left = new BigInteger(x);
- BigInteger right = new BigInteger(y);
- BigInteger one = new BigInteger("1");
- int count = 0;
- while(!(left.compareTo(one) ==0 && right.compareTo(one) == 0)){
- if (left.compareTo(right) > 0){
- left = left.subtract(right);
- System.out.println("==============");
- System.out.println(left);
- count++;
- }
- else if (left.compareTo(right) < 0){
- right = right.subtract(left);
- System.out.println("&&&&&&&&&&&&&&&&&");
- System.out.println(right);
- count++;
- }
- else{
- System.out.println("Impossible");
- }
- }
- System.out.println(count);
- }
- }
复制代码
这个code有问题,当一个数特别大,另一个数特别小的时候,会消耗很多的时间,因为每次只从那个较大的数减去较小的数,系统会run out of time and finally it will fail
所以怎么办呢,要相处一个办法来处理两个相差很大的数
Here is the solution:
|
|