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;
}
相关推荐
坚持编程的菜鸟40 分钟前
LeetCode每日一题——困于环中的机器人
c语言·算法·leetcode·机器人
Aurorar0rua2 小时前
C Primer Plus Notes 09
java·c语言·算法
R-G-B4 小时前
【02】C#入门到精通——C# 变量、输入/输出、类型转换
开发语言·c#·c# 变量·c#输入/输出·c#类型转换
星河队长4 小时前
C# 软件加密方法,有使用时间限制,同时要防止拷贝
开发语言·c#
史迪奇_xxx4 小时前
10、一个简易 vector:C++ 模板与 STL
java·开发语言·c++
2301_801252224 小时前
Java中的反射
java·开发语言
Kiri霧5 小时前
Rust开发环境搭建
开发语言·后端·rust
weixin-a153003083165 小时前
[数据抓取-1]beautifulsoup
开发语言·python·beautifulsoup
我不是QI5 小时前
DES 加密算法:核心组件、加解密流程与安全特性
经验分享·算法·安全·网络安全·密码学
前端小刘哥5 小时前
新版视频直播点播EasyDSS平台,让跨团队沟通高效又顺畅
算法