2019年[海淀区赛 第2题] 阶乘

题目描述

n的阶乘定义为n!=n*(n -1)* (n - 2)* ...* 1。n的双阶乘定义为n!!=n*(n -2)* (n -4)* ...* 2或n!!=n(n - 2)*(n - 4)* ...* 1取决于n的奇偶性,但是阶乘的增长速度太快了,所以我们现在只想知道n!和n!!末尾的的个数

输入格式

一个正整数n (n <= )

输出格式

两个整数分别为n!和n!!末尾0的个数

样例输入#1

10

样例输出#1

2 1

样例输入#2

5

样例输出#2

1 0

参考代码

cpp 复制代码
#include <iostream>
#include <cstdio>
#define ll long long
using namespace std;

int main()
{
	freopen("factorial.in", "r", stdin); //此处为海淀区赛指定文件名
	freopen("factorial.out", "w", stdout);//考场代码,必须加文件输入输出
	
	ll n, cnt_5, cnt_2;
	scanf("%lld", &n);
	
	cnt_5 = cnt_2 = 0;
	
	for(ll i = 1; i <= n; i++)
	{
		ll tmp = i;
		
		while(tmp % 5 == 0)
		{
			cnt_5++;
			tmp /= 5;
		}
		
		tmp = i;
		 
		while(tmp % 2 == 0)
		{
			cnt_2++;
			tmp /= 2;
		}
	}
	
	printf("%lld ", min(cnt_2, cnt_5));
	cnt_5 = cnt_2 = 0;

	if(n % 2 != 0)
	{
		for(ll i = n; i >= 1; i -= 2)
		{
			ll tmp = i;
			
			while(tmp % 5 == 0)
			{
				cnt_5++;
				tmp /= 5;
			}
			
			tmp = i;
			 
			while(tmp % 2 == 0)
			{
				cnt_2++;
				tmp /= 2;
			}
		}
	}
	else
	{
		for(ll i = n; i >= 2; i -= 2)
		{
			ll tmp = i;
			
			while(tmp % 5 == 0)
			{
				cnt_5++;
				tmp /= 5;
			}
			
			tmp = i;
			 
			while(tmp % 2 == 0)
			{
				cnt_2++;
				tmp /= 2;
			}
		}
	}
	
	printf("%lld", min(cnt_5, cnt_2));
	
	return 0;
}
相关推荐
睡美人的小仙女1274 小时前
Threejs加载环境贴图报错Bad File Format: bad initial token
开发语言·javascript·redis
rayufo5 小时前
【工具】列出指定文件夹下所有的目录和文件
开发语言·前端·python
RANCE_atttackkk5 小时前
[Java]实现使用邮箱找回密码的功能
java·开发语言·前端·spring boot·intellij-idea·idea
数研小生5 小时前
构建命令行单词记忆工具:JSON 词库与艾宾浩斯复习算法的完美结合
算法·json
芒克芒克5 小时前
LeetCode 题解:除自身以外数组的乘积
算法·leetcode
缺点内向5 小时前
C#编程实战:如何为Word文档添加背景色或背景图片
开发语言·c#·自动化·word·.net
一起养小猫5 小时前
Flutter for OpenHarmony 实战:记账应用数据统计与可视化
开发语言·jvm·数据库·flutter·信息可视化·harmonyos
zhougl9966 小时前
Java 所有关键字及规范分类
java·开发语言
Python 老手6 小时前
Python while 循环 极简核心讲解
java·python·算法
@Aurora.6 小时前
优选算法【专题九:哈希表】
算法·哈希算法·散列表