HODJ Big Number(java 阶乘)
来源:互联网 发布:获取百度指数的源数据 编辑:程序博客网 时间:2024/06/06 03:08
Big Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24822 Accepted Submission(s): 11266
Problem Description
In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.
Input
Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 ≤ n ≤ 107 on each line.
Output
The output contains the number of digits in the factorial of the integers appearing in the input.
Sample Input
21020
Sample Output
719超时代码:以为用大数可以解决,结果发现求千万数量级的阶乘内存也无法受得了import java.math.BigInteger;import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); while(n-->0){ String str = in.next(); BigInteger number = new BigInteger(str); BigInteger sum = BigInteger.ONE; while(number.intValue()>0){ sum = sum.multiply(number); number = number.subtract(BigInteger.ONE); } System.out.println(sum.toString().length()); } }}
AC代码1:利用数学中对数公式可以求解出,lg(N!)=lg(1)+lg(2)+…+lg(N)import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); while(n-->0){ double d = in.nextDouble(); double sum = 0; for(int i=1; i<=d; i++){ sum += Math.log10(i); } System.out.println((int)sum+1); } }}AC代码2:采用斯特林公式计算能够在时间上快速解决链接:http://baike.baidu.com/view/4113061.htm?fr=aladdinimport java.util.Scanner;public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); while(n-->0){ int number = in.nextInt(); double sum1 = Math.log10(Math.sqrt(2*Math.PI*number)); double sum2 = number*Math.log10(number/Math.E); System.out.println((int)(sum1+sum2+1)); } }}
0 0
- HODJ Big Number(java 阶乘)
- Big Number(阶乘)
- POJ 1423 Big Number 阶乘位数+Stirling+JAVA .
- 求阶乘--hdu1018--Big Number
- 阶乘位数—Big Number
- hdu(1018)Big Number(阶乘位数的计算)
- 求大数阶乘的位数(ACM Big Number问题)
- Big Number(求阶乘数的位数)
- zoj 1526 Big Number (N阶乘的位数)
- hdu1018——Big Number(大数阶乘,斯特林公式)
- hdu 1018 Big Number(公式求阶乘位数)
- HDU--1018--Big number!(计算阶乘结果的位数)
- pku 1423 阶乘 Big Number 解题报告
- ACMSTEP 2.3.3 Big Number //阶乘位数
- Poj 1423 Big Number//数学,阶乘
- Big Number(求任意阶乘的位数)
- HDU1018-Big Number(阶乘的位数)
- HDU1018 Big Number(求阶乘位数)
- C语言基础概况笔记
- 判断一个服务是否处于开启状态
- php 会员模块 如果用户未登录,强行跳转至登录页
- 51单片机(HC6800—EM3 V2.2)数码管操作——静态显示
- 要全面啊~
- HODJ Big Number(java 阶乘)
- /tmp/mathworks_15227/java/jre/glnxa64/jre/bin/java: Permission denied
- 近期接触到的开源工具总结
- QT——QSqlRelationalTableModel 、 QSqlRelationalDelegate
- 正则表达式数据验正
- VS2008启动调试加载进程卡死
- 高性能服务总结
- 角色技能攻击范围点区域显示
- 终于到了今天