69-数的长度

内存限制:64MB
时间限制:3000ms
特判: No

通过数:10
提交数:13
难度:1

题目描述:

N!阶乘是一个非常大的数,大家都知道计算公式是N!=N*(N-1)······*2*1.现在你的任务是计算出N!的位数有多少(十进制)?

输入描述:

首行输入n,表示有多少组测试数据(n<10)
随后n行每行输入一组测试数据 N( 0 < N < 1000000 )

输出描述:

对于每个数N,输出N!的(十进制)位数。

样例输入:

复制

3
1
3
32000

样例输出:

1
1
130271

分析:
  1、n个数相乘,最终得到的数它的位数为n个数每个数进行log10()的运算再求和,最后加上1;
  2、ps:用Java做大数乘法最后来数位数,会超时; C/C++代码实现(AC):
 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <stack>
#include <map>
#include <queue>
#include <set> using namespace std; int main()
{
int t;
scanf("%d", &t);
while(t --)
{
double a, ans = ;
scanf("%lf", &a);
for(int i = ; i <= a; ++ i)
ans += log10(i);
printf("%d\n", int(ans) + );
}
return ;
}

Java代码(TLE):

 import java.util.*;
import java.io.*;
import java.math.*; public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while (t > 0) {
t--;
int k = scan.nextInt();
BigInteger a = BigInteger.ONE;
for(int i = 1; i <= k; ++ i) {
a = a.multiply(BigInteger.valueOf(i));
}
String str = a.toString();
System.out.println(str.length());
}
}
}

最新文章

  1. 浅谈JSON.stringify 函数与toJosn函数和Json.parse函数
  2. [UCSD白板题] Changing Money
  3. ArduinoYun的电源插座
  4. Gevent的长轮询实现方法详解
  5. MVC5 Bundles发布到IIS失效问题解决方案
  6. HTML中标签和元素的区别
  7. 关于VNC黑屏的问题
  8. linux upstart启动配置
  9. IO之同步、异步、阻塞、非阻塞
  10. ArrayList和LinkedList源码
  11. 敏捷视界:Scrum起源、Scrum术语
  12. 一、Python3.6+PyQt5 安装
  13. Python基础 列表介绍、使用
  14. 常见SMTP发送失败原因列表
  15. 报错Domain=NSCocoaErrorDomain Code=3840 &quot;Garbage at end.&quot;
  16. 模板std::mutex用法:
  17. Chapter 4 Invitations——1
  18. Python内置类型(1)——真值测试
  19. c语言亲缘线程通过管道通信一些疑问
  20. 吴裕雄 29-MySQL 处理重复数据

热门文章

  1. css 文字间距
  2. prototype与 _proto__的关系
  3. [Luogu3932] 浮游大陆的68号岛
  4. USACO环绕岛屿Surround the Islands 并查集 枚举暴力
  5. vue element NavMenu 莫名出现蓝色边框
  6. ThreadPoolExecutor使用方法
  7. python-写入文件
  8. 【洛谷】P2371 [国家集训队]墨墨的等式(屠版题)
  9. IoTClient开发4 - ModBusTcp协议服务端模拟
  10. 最新JetBrains PyCharm 使用教程--常用功能设置(三)