Big Number
来源:互联网 发布:手机c语言表白代码 编辑:程序博客网 时间:2024/04/27 21:55
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
Source
Asia 2002, Dhaka (Bengal)
Recommend
JGShining
答案:
#include <stdio.h>
#include <cmath>
int main()
{
int n, t, i;
double sum;
if (scanf("%d", &t) && t)
{
while (t--)
{
scanf("%d", &n);
sum = 0;
for (i = 1; i <= n; ++i)
{
sum += log10((double) i);
}
printf("%d/n", (long)ceil(sum));
}
}
return 0;
}
{
int n, t, i;
double sum;
if (scanf("%d", &t) && t)
{
while (t--)
{
scanf("%d", &n);
sum = 0;
for (i = 1; i <= n; ++i)
{
sum += log10((double) i);
}
printf("%d/n", (long)ceil(sum));
}
}
return 0;
}
下面是另一种算法,不过超时了,并且处理位数有限,但是可以求出相关的阶乘数:
#include <string>
int main()
{
long n, s, tmp;
long v;
char sum[100000000];
int j, w;
scanf("%d", &n);
while (n--)
{
scanf("%d", &v);
memset(sum, 0, sizeof(sum));
sum[0] = '1';
for (j = 2; j <= v; ++j)
{
s = 0;
for (w = 0; w < strlen(sum); ++w)
{
tmp = j*(sum[w]-'0')+s;
s = tmp/10;
sum[w] = tmp%10+'0';
}
if (s > 0)
{
do
{
sum[w++] = s%10+'0';
s = s/10;
}while (s > 0);
}
}
printf("%d/n", strlen(sum));
}
{
long n, s, tmp;
long v;
char sum[100000000];
int j, w;
scanf("%d", &n);
while (n--)
{
scanf("%d", &v);
memset(sum, 0, sizeof(sum));
sum[0] = '1';
for (j = 2; j <= v; ++j)
{
s = 0;
for (w = 0; w < strlen(sum); ++w)
{
tmp = j*(sum[w]-'0')+s;
s = tmp/10;
sum[w] = tmp%10+'0';
}
if (s > 0)
{
do
{
sum[w++] = s%10+'0';
s = s/10;
}while (s > 0);
}
}
printf("%d/n", strlen(sum));
}
return 0;
}
}
- big big number 求和
- big number
- Big Number
- Big Number
- big number
- Big Number
- Big Number
- Big Number
- Big Number
- Big Number
- Big Number
- Big Number
- Big Number
- Big Number
- Big Number
- Big Number
- Big Number
- Big Number
- unbutu下添加桌面图标
- SharpStreaming项目开发纪实:项目源码发布
- UpdateData()用法
- 未定义行为
- 巧妙使用SQL Profiler监控服务器
- Big Number
- Oracle中表数据导出到excel中
- 我的作品(EOS 7D):再上传也麻烦,也浪费资源,就这样吧: http://bbs.pcpop.com/101007/9200043.html
- CSS 3 First Look -
- MoreLikeThis实现检索相似文档
- Least Common Multiple
- 我的作品之二 (EOS 7D):再上传也麻烦,也浪费资源,就这样吧~
- huawei--创建表,用表分区、表空间、索引、主见
- 启动Tomcat6.x时manager does not exist or is not a readable directory