第九届蓝桥杯C++B组 乘积尾零 BigInteger!!

描述如下的10行数据,每行有10个整数,请你求出它们的乘积的末尾有多少个零?
5650 4542 3554 473 946 4114 3871 9073 90 43292758 7949 6113 5659 5245 7432 3051 4434 6704 35949937 1173 6866 3397 4759 7557 3070 2287 1453 98991486 5722 3135 1170 4014 5510 5120 729 2880 90192049 698 4582 4346 4427 646 9742 7340 1230 76835693 7015 6887 7381 4172 4341 2909 2027 7355 56496701 6645 1671 5978 2704 9926 295 3125 3878 67852066 4247 4800 1578 6652 4616 1113 6205 3264 29153966 5291 2904 1285 2193 1428 2265 8730 9436 7074689 5510 8243 6114 337 4096 8199 7313 3685 211
解答思路:这么多数乘积必定要用BigInteger了,正好通过这个题熟练一下大整数的用法
我是直接求出的乘积然后数的,但我写了个方法计算0个数的,但是没运行出来,所以直接上手指头写了

public static void main(String args[]){
    Scanner s=new Scanner(System.in);
    int a[][]=new int[10][10];
    ArrayList<BigInteger> al=new ArrayList<BigInteger>();
    for(int i=0;i<10;i++){
    for(int j=0;j<10;j++){
      a[i][j]=s.nextInt();
      al.add(BigInteger.valueOf(a[i][j]));
   }
    }
    BigInteger sum=new BigInteger("1");
  for(int i=0;i<100;i++){
  sum=(BigInteger)sum.multiply(al.get(i));
  }
  System.out.println(sum);
  /*以下方法我感觉应该是对的啊,但是count最后没有输出结果*/
//  int count=0;
//  BigInteger a1=new BigInteger("0");
//  BigInteger a2=new BigInteger("10");
//  while(sum.compareTo(a1)>=0){
//  if(sum.remainder(a2)==a1){
//   count++;
//  }
//    
//   sum=sum.divide(a2);
//   }
  System.out.println(31);
   }

嗯…最后答案就是31
下面试运行出来的部分结果(包括最后的0)

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值