求1000!的结果末尾有多少个0 1000! = 1×2×3×4×5×...×999×1000

笨笨的菜鸟 2014-02-12 04:36:57
求1000!的结果末尾有多少个0
1000! = 1×2×3×4×5×...×999×1000
...全文
906 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
__murphy__ 2015-08-04
  • 打赏
  • 举报
回复
int numberzero(int n){
	int sum = 0;
	int count = 5;
	while (count <= n){
		sum += n / count;
		count = count * 5;
	}
	return sum;
}
卡卡吉利 2014-02-12
  • 打赏
  • 举报
回复
引用 8 楼 sunbo624 的回复:
[quote=引用 5 楼 zuoziji_lj 的回复:] [quote=引用 3 楼 sunbo624 的回复:]

public static void main(String[] args) {
		
		BigInteger result = new BigInteger("1");
		
		for(int i = 1, j = 1; i <= 1000; i++, j = i % 10) {
			
			if(j == 0 || j == 2 || j == 5) {
				
				result = result.multiply(new BigInteger(String.valueOf(i)));
				
			}
			
		}
		
		String str = result.toString();
		
		int len = 0;
		
		for(int i = str.length() - 1; i > -1; i--) {
			
			if(str.charAt(i) != 0x30) {
				break;
			}
			
			len++;
			
		}
		
		System.out.println(len);
		
	}
求的是末尾有多少个0,不是总共有多少个0[/quote] 我求的就是末尾 if(str.charAt(i) != 0x30) break; 这个就是当发现非0的元素 结束循环 循环是倒着做的 测试没问题 这个不用看了 没优化 刚才又写了一个优化的[/quote]哦,知道自己错在哪里了,50 500这些情况没算进去
sunbo624 2014-02-12
  • 打赏
  • 举报
回复
引用 5 楼 zuoziji_lj 的回复:
[quote=引用 3 楼 sunbo624 的回复:]

public static void main(String[] args) {
		
		BigInteger result = new BigInteger("1");
		
		for(int i = 1, j = 1; i <= 1000; i++, j = i % 10) {
			
			if(j == 0 || j == 2 || j == 5) {
				
				result = result.multiply(new BigInteger(String.valueOf(i)));
				
			}
			
		}
		
		String str = result.toString();
		
		int len = 0;
		
		for(int i = str.length() - 1; i > -1; i--) {
			
			if(str.charAt(i) != 0x30) {
				break;
			}
			
			len++;
			
		}
		
		System.out.println(len);
		
	}
求的是末尾有多少个0,不是总共有多少个0[/quote] 我求的就是末尾 if(str.charAt(i) != 0x30) break; 这个就是当发现非0的元素 结束循环 循环是倒着做的 测试没问题 这个不用看了 没优化 刚才又写了一个优化的
sunbo624 2014-02-12
  • 打赏
  • 举报
回复
刚才那个粗略写的没优化 又写个效率高点的

public static void main(String[] args) {
		
		int max = 1000, temp = 1, sum = 0;
		
		for(int i = 1, j = 1; i <= max; i++, j = i % 10) {
			
			if(j == 0 || j == 2 || j == 5) {
				
				temp *= i;
				
				while(temp % 10 == 0) {
					sum++;
					temp /= 10;
				}
				
				temp %= max;
				
			}
			
		}
		
		System.out.println(sum);
		
	}
  • 打赏
  • 举报
回复
这是一个竞赛的题目。原题是100的阶层。 思路:一个数 n 的阶乘末尾有多少个 0 取决于从 1 到 n 的各个数的因子中 2 和 5 的个数, 而 2 的个数是远远多余 5 的个数的, 因此求出 5 的个数即可。 1000/5=200 1000/25=40 1000/125=8 1000、625=1 200+40+8+1=249
卡卡吉利 2014-02-12
  • 打赏
  • 举报
回复
引用 3 楼 sunbo624 的回复:

public static void main(String[] args) {
		
		BigInteger result = new BigInteger("1");
		
		for(int i = 1, j = 1; i <= 1000; i++, j = i % 10) {
			
			if(j == 0 || j == 2 || j == 5) {
				
				result = result.multiply(new BigInteger(String.valueOf(i)));
				
			}
			
		}
		
		String str = result.toString();
		
		int len = 0;
		
		for(int i = str.length() - 1; i > -1; i--) {
			
			if(str.charAt(i) != 0x30) {
				break;
			}
			
			len++;
			
		}
		
		System.out.println(len);
		
	}
求的是末尾有多少个0,不是总共有多少个0
卡卡吉利 2014-02-12
  • 打赏
  • 举报
回复
引用 2 楼 u013336828 的回复:
不对呢!!怎么是210,应该是249
答案在哪里?
sunbo624 2014-02-12
  • 打赏
  • 举报
回复

public static void main(String[] args) {
		
		BigInteger result = new BigInteger("1");
		
		for(int i = 1, j = 1; i <= 1000; i++, j = i % 10) {
			
			if(j == 0 || j == 2 || j == 5) {
				
				result = result.multiply(new BigInteger(String.valueOf(i)));
				
			}
			
		}
		
		String str = result.toString();
		
		int len = 0;
		
		for(int i = str.length() - 1; i > -1; i--) {
			
			if(str.charAt(i) != 0x30) {
				break;
			}
			
			len++;
			
		}
		
		System.out.println(len);
		
	}
笨笨的菜鸟 2014-02-12
  • 打赏
  • 举报
回复
不对呢!!怎么是210,应该是249
卡卡吉利 2014-02-12
  • 打赏
  • 举报
回复
public static void main(String[] args){
		int sum = 0;
		for(int i=1;i<=1000;i++){
			if(i%10 == 5){
				sum++;
			}
			if(i%10 == 0){
				if(i%100 == 0){
					if(i%1000 == 0){
						sum += 3;
					}else{
						sum +=2;
					}
				}else{
					sum +=1;
				}
			}
		}
		System.out.println(sum);
	}

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧