求后倒零

Posted rstz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求后倒零相关的知识,希望对你有一定的参考价值。

题目:求N!末尾有多少个零。


题目分析:一般先想到的是算出N!然后在对其求余,没错会爆,数据是以指数型增长。但是这个时候又可以想到,因为N!是十进制,所以每有一队 2,5就可以生一个零。所以我们只需要统计

2和5的个数然后取最小就可以得到后倒零。

  1 #include <iostream>
  2 #include <algorithm>
  3 using namespace std;
  4 typedef long long LL;
  5 int main(){
  6 	LL n;
  7 	cin >> n;
  8 	LL _2 = 0, _5 = 0;
  9 	for(LL i = 2; i <= n; ++ i){
 10 		LL t = i;
 11 		while(t % 5 == 0){
 12 			_5++;
 13 			t /= 5;
 14 		}
 15 		while(t % 2 == 0){
 16 			_2++;
 17 			t >>= 1;
 18 		}
 19 	}
 20 	cout << min(_2, _5) << endl;
 21 	return 0;
 22 }



另一样的题:

技术图片

  1 #include <iostream>
  2 #include <algorithm>
  3 using namespace std;
  4 int main(){
  5 	int _2 = 0, _3 = 0;
  6 	int a;
  7 	int n;
  8 	cin >> n;
  9 	for(int i = 0; i < n; ++ i){
 10 		cin >> a;
 11 		int t = a;
 12 		while(t % 2 == 0){
 13 			t >>= 1;
 14 			_2 ++;
 15 		}
 16 		t = a;
 17 		while(t % 3 == 0){
 18 			t /= 3;
 19 			_3 ++;
 20 		}
 21 
 22 	}
 23 	cout << min(_2, _3) << endl;
 24 
 25 }

以上是关于求后倒零的主要内容,如果未能解决你的问题,请参考以下文章

已知前序中序求后序-二叉树

codevs 2010 求后序遍历x

已知先序遍历和中序遍历,求后序遍历

POJ2255-已知二叉树前序中序求后序

已知先序和中序 求后序

求后序遍历