北理工java第五周

  1. import java.util.Scanner;  
    import java.math.BigInteger;  
    public class Main {  
        public static void main(String[] args){  
            Scanner scanner = new Scanner(System.in);  
            String num;  
            num = scanner.next();  
            BigInteger n=new BigInteger(num);  
            BigInteger a=BigInteger.valueOf(1);  
            BigInteger b=BigInteger.valueOf(0);  
            BigInteger ans;  
            while(true){  
                if(n.compareTo(BigInteger.valueOf(0))==0){  
                    ans=b;  
                    break;  
                }  
                if(n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(0))!=0){  
                    b=a.add(b);  
                    n=n.divide(BigInteger.valueOf(2));  
                }  
                else if(n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(0))==0){  
                    a=a.add(b);  
                    n=n.divide(BigInteger.valueOf(2));  
                }  
            }  
            System.out.println(ans);  
        }  
    }  

第二题就是循环加Biginteger

  1. import java.util.Scanner;  
    import java.math.BigInteger;  
    public class Main {  
        public static void main(String[] args){  
            Scanner sc = new Scanner(System.in);  
            String num;  
            num=sc.next();  
            BigInteger b=new BigInteger(num);  
            BigInteger c=BigInteger.valueOf(1);  
            BigInteger ans=BigInteger.valueOf(1);  
            while(b.compareTo(c)==1){  
                ans=ans.multiply(c);  
                c=c.add(BigInteger.valueOf(1));  
            }  
            ans=ans.multiply(b);  
            System.out.println(ans);  
      
        }  
      
      
      
    }  

第三题就是字符串处理加biginterger

·  import java.math.BigInteger;    

·  import java.util.Scanner;    

·      

·  public class Main {    

·      static int[] coefficient = new int[100];    

·      static int[] index = new int[100];    

·      static int max_index = 0;    

·      public static void fun (String poly){    

·          String xi = "", zhi = "";    

·          boolean count_x = false;    

·          char[] item = poly.toCharArray();    

·      

·          if (poly.compareTo("-x") == 0) {    

·              coefficient[1] = -1;    

·              index[1] = 1;    

·              return;    

·          } else if (poly.compareTo("x") == 0) {    

·              coefficient[1] = 1;    

·              index[1] = 1;    

·              return;    

·          }    

·          for (int i = 0; i < item.length; i++) {    

·              if(item[i] == 'x' && i == item.length-1) {    

·                  coefficient[1] = Integer.parseInt(xi);    

·                  index[1] = 1;    

·                  return;    

·              } else if (item[i] == 'x') {    

·                  count_x = true;    

·                  i++;    

·              } else if (count_x == false) {    

·                  xi += item[i];    

·              } else if (count_x == true) {    

·                  zhi += item[i];    

·              }    

·          }    

·      

·          int zhi_temp = 0;    

·          if (zhi != "") {    

·              zhi_temp = Integer.parseInt(zhi);    

·          } else if (zhi == "" && count_x == false) {    

·              zhi_temp = 0;    

·          }    

·      

·          index[zhi_temp] = zhi_temp;    

·          if (xi.length() == 0) {    

·              coefficient[zhi_temp] = 1;    

·          } else if (xi.equals("-")){    

·              coefficient[zhi_temp] = -1;    

·          } else {    

·              coefficient[zhi_temp] = Integer.parseInt(xi);    

·          }    

·          max_index = Math.max(max_index, zhi_temp);    

·      }    

·      

·      public static void main (String[] args) {    

·          Scanner sc = new Scanner(System.in);    

·          String temp = sc.nextLine();    

·          BigInteger n = sc.nextBigInteger();    

·          char[] item = temp.toCharArray();    

·      

·          String poly = "";    

·          for (int i = 0; i < item.length; i++) {    

·              if ((item[i] == '+' || item[i] == '-') && i != 0) {    

·                  fun (poly);    

·                  poly = "";    

·                  if (item[i] == '-') {    

·                      poly += '-';    

·                  }    

·      

·              } else {    

·                  poly += item[i];    

·              }    

·          }    

·          fun (poly);    

·          /*  

·          for (int i = 0; i <= max_index; i++){  

·              System.out.print(coefficient[i]);  

·              System.out.print(" ");  

·              System.out.println(index[i]);  

·          }  

·          */    

·      

·          for (int i = 1; i <= max_index; i++) {    

·              if(index[i] != 0) {    

·                  coefficient[i] *= i;    

·                  index[i] --;    

·              }    

·          }    

·          /*  

·          for (int i = 1; i <= max_index; i++){  

·              System.out.print(coefficient[i]);  

·              System.out.print(" ");  

·              System.out.println(index[i]);  

·          }  

·          */    

·      

·          BigInteger ans = BigInteger.valueOf(0);    

·          for (int i = 1; i <= max_index; i++) {    

·              BigInteger coe1, coe2;    

·              coe1 = n.pow(index[i]);    

·              coe2 = coe1.multiply(BigInteger.valueOf(coefficient[i]));    

·              ans = ans.add(coe2);    

·          }    

·          System.out.println(ans);    

·      

·      

·      }    

·      

·  } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值