HPU 1166: 阶乘问题(一)
1166: 阶乘问题(一) [数学]
时间限制: 1 Sec 内存限制: 128 MB
提交: 58 解决: 24 统计
题目描述
输入
每组数据输入一个整数NN。
注:1<=T<=10,1<=N<=2∗1061<=T<=10,1<=N<=2∗106。
输出
样例输入
2
100
1000
样例输出
158
2568
方法一:利用斯特林公式 
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#define Pi acos(-1)
#define e exp(double(1))
using namespace std;
int main()
{
int t,n,sum;
cin>>t;
while(t--)
{
cin>>n;
sum=floor(log10(sqrt(2*Pi*n))+n*log10(n/e))+1;
//floor()向下取整。即取不大于x的最大整数
if(n==1) sum=1;
cout<<sum<<endl;
}
return 0;
}
方法二:无视公式,暴力
log10(n!)log10(n!)
=log10(1∗2∗3…∗n)=log10(1∗2∗3…∗n)
=log10(1)+log10(2)+…+log10(n)
#include<cstdio>
#include<cmath>
int main()
{
int n,i,t;
double d;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1,d=0;i<=n;i++) d+=log10(i);
printf("%d\n",(int)d+1);
}
return 0;
}
HPU 1166: 阶乘问题(一)的更多相关文章
- HPU 第三次积分赛:阶乘之和(水题)
阶乘之和 描述 对于整数pp,给出以下定义 p=x_{1}!+x_{2}!+x_{3}!+...+x_{q}!(x_{i}<x_{j}for\ all\ i<j )p=x1!+x2!+ ...
- C语言 · 阶乘计算 · 基础练习
问题描述 输入一个正整数n,输出n!的值. 其中n!=1*2*3*-*n. 算法描述 n!可能很大,而计算机能表示的整数范围有限,需要使用高精度计算的方法.使用一个数组A来表示一个大整数a,A[0]表 ...
- Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes
题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- 关于for循环的几个小练习,例如奇数偶数,阶乘,求和等
1 .100以内的奇数和偶数 var js = ""; var os = ""; for(var i=1;i<101;i++) { if(i%2 == 0 ...
- [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 求单链表L各结点的阶乘之和(c语言)
链表需要用到指针 阶乘需要用到递归 链表中的注意事项: 1.链表L是否等于NULL ----------是循环结束的条件 2.链表L->Data ---------取链表L中各个结点的值 3.L ...
- 洛谷 P2726 阶乘 Factorials Label:Water
题目背景 N的阶乘写作N!,表示小于等于N的所有正整数的乘积. 题目描述 阶乘会变大得很快,如13!就必须用32位整数类型来存储,到了70!即使用浮点数也存不下了. 你的任务是找到阶乘最前面的非零位. ...
- js实现阶乘
//while循环实现function calNum(n) { var product = 1; while(n > 1){//1*5*4*3*2,1*n*(n-1)*(n-2)*...*2 p ...
- 洛谷P1134 阶乘问题[数论]
题目描述 也许你早就知道阶乘的含义,N阶乘是由1到N相乘而产生,如: 12! = 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 x 11 x 12 = 479,001, ...
随机推荐
- c++ primer plus 第五章 课后题答案
#include <iostream> using namespace std; int main() { ; cout << "Please enter two n ...
- 监控视频采集与Web直播开发全流程分析
内容概要: 摄像头 => FFmpeg => Nginx服务器 => 浏览器 从摄像头拉取rtsp流 转码成rtmp流向推流服务器写入 利用html5播放 1.开发流程 1.1 通过 ...
- HDU 6098 Inversion
Inversion 思路:从大到小排序后,每次找到第一个下标不整出i的输出. 代码: #include<bits/stdc++.h> using namespace std; #defin ...
- rxjs学习
推荐一个好的网站:http://cn.rx.js.org/manual/overview.html#- https://rxjs-cn.github.io/learn-rxjs-operators/o ...
- Illumina Sequence Identifiers 序列标识符 index详解
大家基本都知道什么是 FASTA 和 FastQ 格式了,但这是不够的. 我们还需要了解世界上最大的测序公司自己定制的 FastQ 格式,因为你可能会经常用到,有时还会亲自去处理它们. 本文主题:Il ...
- 在 Confluence 6 中的 Jira 权限
只读(Read Only) 从你 JIRA 应用服务器上取得的用户,用户组和用户组成员.这些用户的信息只能通过你的 JIRA 服务器进行修改. https://www.cwiki.us/display ...
- Non-parseable POM 解决方法
两个build放在一起当然不行,把它们类似这样的合并起来.
- Leetcode 82
有个错误就是member access within null pointer of type 'struct ListNode' 其实就是判断了指针是否异常了,比如NULL->next之类.要 ...
- dp练习(5)——最长严格上升子序列
1576 最长严格上升子序列 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 给一个数组a1, a2 ... ...
- win10激活工具---KMSAutoNet
win10激活工具---KMSAutoNet 1> 2> 3> 4> 5> 6> 8>