黑马程序员技术交流社区

标题: 【编程挑战(新人)】 [打印本页]

作者: 返璞归真    时间: 2014-11-15 13:46
标题: 【编程挑战(新人)】
求1000的阶乘结果中所有零的个数。{:2_41:}
作者: 返璞归真    时间: 2014-11-15 13:48
欢迎大家来讨论哈,说说自己的想法也好
作者: kang3214    时间: 2014-11-15 14:02
用递归决解
作者: 返璞归真    时间: 2014-11-15 14:05
kang3214 发表于 2014-11-15 14:02
用递归决解

兄弟啊,递归不行的,结果的位数2000多位。。。
作者: 福禄娃    时间: 2014-11-15 20:54
  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. }
复制代码


一切尽在代码之中~。
作者: 田峻菘    时间: 2014-11-15 22:37
我的测试题答案:
  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. }
复制代码

作者: 返璞归真    时间: 2014-11-16 06:54
使用数组也可以解决(不用BigInteger类),大家可以尝试下哈




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2