FOJ 1050 Number lengths

一,题目描述

 

二,问题分析

1.题目意思为输入一个整数N 输入 N!的位数

2.很明显常规方法无法实现,当N很大时,N!的阶乘就无法表示出来

3.考虑数学方法 位数ans = log10(N!) =  ∑(1≤i≤n)log(i) ,取整后+1

三,代码解答

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

//公式:ans = log10(n!) =  ∑(1≤i≤n)log(i).

int main() {
	int n;
	while (cin >> n) {
		double res = 0;
		while (n) {
			res = res + log10((double)n);		
			n--;
		}
		cout << (int)res + 1 << endl;;
	}
	return 0;
}

注意:1.用log10()函数需要引入头文件#include<cmath>

2.根据函数声明,其参数要使用duoble类型

发布了54 篇原创文章 · 获赞 14 · 访问量 3583

猜你喜欢

转载自blog.csdn.net/q2511130633/article/details/105071631