A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© BackGaoz 中级黑马   /  2016-4-23 21:27  /  745 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/**
   *
   *  需求:求出1000的阶乘所有零和尾部零的个数,不用递归做
  1. package com.heima.test;

  2. import java.math.BigInteger;

  3. public class Test6 {

  4.         public static void main(String[] args) {
  5.                 /*int result = 1;
  6.                 for(int i = 1; i <= 1000; i++) {
  7.                         result = result * i;
  8.                 }
  9.                
  10.                 System.out.println(result);                //因为1000的阶乘远远超出了int的取值范围
  11.                 */
  12.                 //demo1();
  13.                 demo2();
  14.         }

  15.         public static void demo2() {                //获取1000的阶乘尾部有多少个零
  16.                 BigInteger bi1 = new BigInteger("1");
  17.                 for(int i = 1; i <= 1000; i++) {
  18.                         BigInteger bi2 = new BigInteger(i+"");
  19.                         bi1 = bi1.multiply(bi2);        //将bi1与bi2相乘的结果赋值给bi1
  20.                 }
  21.                 String str = bi1.toString();        //获取字符串表现形式
  22.                 StringBuilder sb = new StringBuilder(str);
  23.                 str = sb.reverse().toString();        //链式编程
  24.                
  25.                 int count = 0;                                        //定义计数器
  26.                 for(int i = 0; i < str.length(); i++) {
  27.                         if('0' != str.charAt(i)) {
  28.                                 break;
  29.                         }else {
  30.                                 count++;
  31.                         }
  32.                 }
  33.                
  34.                 System.out.println(count);
  35.         }

  36.         public static void demo1() {                //求1000的阶乘中所有的零
  37.                 BigInteger bi1 = new BigInteger("1");
  38.                 for(int i = 1; i <= 1000; i++) {
  39.                         BigInteger bi2 = new BigInteger(i+"");
  40.                         bi1 = bi1.multiply(bi2);        //将bi1与bi2相乘的结果赋值给bi1
  41.                 }
  42.                 String str = bi1.toString();        //获取字符串表现形式
  43.                 int count = 0;
  44.                 for(int i = 0; i < str.length(); i++) {
  45.                         if('0' == str.charAt(i)) {        //如果字符串中出现了0字符
  46.                                 count++;                                //计数器加1
  47.                         }
  48.                 }
  49.                 System.out.println(count);
  50.         }

  51. }
复制代码
*/

1 个回复

倒序浏览
再分享一种用递归做的方法
  1. package com.heima.test;

  2. public class Test7 {

  3.         public static void main(String[] args) {
  4.                 System.out.println(fun(1000));
  5.         }

  6.         public static int fun(int num) {
  7.                 if(num > 0 && num < 5) {
  8.                         return 0;
  9.                 }else {
  10.                         return num / 5 + fun(num / 5);
  11.                 }
  12.         }
  13. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马