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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 返璞归真 中级黑马   /  2014-11-15 13:46  /  1010 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

求1000的阶乘结果中所有零的个数。{:2_41:}

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1

查看全部评分

6 个回复

倒序浏览
欢迎大家来讨论哈,说说自己的想法也好
回复 使用道具 举报
kang3214 来自手机 中级黑马 2014-11-15 14:02:32
藤椅
用递归决解
回复 使用道具 举报

兄弟啊,递归不行的,结果的位数2000多位。。。
回复 使用道具 举报
  1. import java.math.*;
  2. /**
  3. * 第9题  求1000!的结果中包含多少个0?注:1000! = 1×2×3×4×5×...×999×1000
  4. */
  5. public class Test9 {

  6.         /*
  7.          * 思路:
  8.          * 1000!这种大数乘法,需要使用BigInteger
  9.          * 先计算出1000!的结果,然后循环取出字符串中的字符,
  10.          * 判断一共有多少个0.
  11.          */
  12.         public static void main(String[] args)
  13.         {
  14.                 String jieChengRes = jieCheng().toString();
  15.                 prin("1000!的运算结果为:"+jieChengRes);
  16.                
  17.                 int zeroCount = returnZeroCount(jieChengRes);
  18.                 prin("结果中一共包含: "+zeroCount+" 个零。");
  19.         }
  20.         
  21.         //1.计算出1000!的结果。
  22.         public static BigInteger jieCheng()
  23.         {
  24.                 BigInteger result = new BigInteger("1");
  25.                
  26.                 for (int i = 1; i <= 1000; i++)
  27.                 {
  28.                         BigInteger num = new BigInteger(String.valueOf(i));
  29.                         
  30.                         // 大数运算与基本数据类型不同,不能使用*运算符,用独有的multiply()方法
  31.                         result = result.multiply(num);
  32.                 }
  33.                
  34.                 return result;
  35.         }
  36.         
  37.         //循环取出字符串中的字符,并记录一共有多少个0.
  38.         public static int returnZeroCount(String res)
  39.         {
  40.                 int count = 0;
  41.                 for (int i = 0; i < res.length(); i++)
  42.                 {
  43.                         String str = String.valueOf(res.charAt(i));
  44.                         if(str.equals("0"))
  45.                         {
  46.                                 count++;
  47.                         }
  48.                 }
  49.                 return count;
  50.         }

  51.         //换行打印通用方法
  52.         public static void prin(Object obj)
  53.         {
  54.                 System.out.println(obj);
  55.         }
  56. }
复制代码


一切尽在代码之中~。
回复 使用道具 举报
我的测试题答案:
  1. package com.itheima;

  2. import java.math.BigInteger;

  3. /**
  4. * 第9题:求1000!的结果中包含多少个0?注:1000! = 1×2×3×4×5×...×999×1000
  5. * 思路:
  6. * 1,创建BigInteger对象
  7. * 2,定义一个变量计数
  8. * 3,for循环计算1000!结果装入BigInteger对象中
  9. * 4,把BigInteger对象转为String类型
  10. * 5,for循环遍历String,若遇'0',则计数变量加1
  11. * 6,打印计数结果
  12. * @author
  13. */
  14. public class Test9 {
  15.         public static void main(String[] args) {
  16.                 // 创建值为1的BigInteger对象factorial
  17.                 BigInteger factorial = new BigInteger("1");
  18.                 // 定义一个变量count计数
  19.                 int count = 0;
  20.                 // for循环计算1000!结果装入factorial中
  21.                 for (int i = 2; i <= 1000; i++) {
  22.                         factorial = factorial.multiply(new BigInteger(i + ""));
  23.                 }
  24.                 // 把factorial转为String类型
  25.                 String s = factorial.toString();
  26.                 // 遍历s,若遇'0',则count++计数
  27.                 for (int i = 0; i < s.length(); i++) {
  28.                         if (s.charAt(i) == '0')
  29.                                 count++;
  30.                 }
  31.                 // 输出计数结果
  32.                 System.out.println("1000!的结果包含0的个数为:" + count);
  33.         }
  34. }
复制代码
回复 使用道具 举报
使用数组也可以解决(不用BigInteger类),大家可以尝试下哈
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马