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类型

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章