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