sdut oj 简单n!

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

给定一个数n(0 <= n <= 150), 求0到n中所有数的阶乘。

输入

题目有多组数据,处理到文件结尾。输入一个数n。

输出

输出阶乘,形式如:4! = 24.每组数据输出后跟一个空行。

示例输入

1
4

示例输出

0! = 1
1! = 1 0! = 1
1! = 1
2! = 2
3! = 6
4! = 24

提示

 代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string> using namespace std; //实现1->150的阶乘 int main()
{
int r[];
int i, j, k, c;
int t;
int n;
while(scanf("%d", &n)!=EOF)
{
if(n==)
{
printf("0! = 1\n\n");
continue;
}
if(n==)
{
printf("0! = 1\n1! = 1\n\n");
continue;
} printf("0! = 1\n1! = 1\n");
for(i=; i<=; i++)
{
r[i]=;
} r[]=j=;
for(i=; i<=n; i++)
{
for(k=; k<j; k++)
{
r[k]=r[k]*i;
}
for(k=c=; k<j; k++ )
{
t=r[k]+c;
r[k]=t%;
c=t/;
}
while(c)
{
r[j]=c%;
c=c/;
j++;
} //处理高位的那部分的进位问题
//printf("%d---\n", j) ;
printf("%d! = ", i );
for(k=j-; k>=; k--)
{
printf("%d", r[k] );
}
printf("\n");
}
printf("\n");
} return ;
} 除法的没涉及!有待添加 /**************************************
Problem id : SDUT OJ 2059
Result : Accepted
Take Memory : 496K
Take Time : 10MS
Submit Time : 2015-01-11 08:22:42
**************************************/

                                               HDU OJ    n!

Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
Input
One N in one line, process to the end of file.
Output
For each N, output N! in one line.
Sample Input
1
2
3
Sample Output
1
2
6
 算法分析:和上一道题目一样也是 大数*小数的数组模拟!
 开二维数组太大开不出来,只好一维每次都要计算一遍。最好是打表存储好1到10000的阶乘数,直接输出就好了,但是10000的阶乘大约有57000位,需要开的内存太大!
 代码:

 #include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <algorithm>
#define mem(a) memset(a, 0, sizeof(a));
using namespace std;
int r[];
int pos[]; void jiecheng(int n)
{
int i, j, k, c, t;
mem(r);
r[]=j=;
for(i=; i<=n; i++)
{
for(k=; k<j; k++)
{
r[k]=r[k]*i; //上一行的数值进行 乘计算
} //逐位进行计算
for(k=c=; k<j; k++) //初始进位数为0
{
t=r[k]+c; //当前值+进位数
r[k]=t%; //取余即为该位数
c=t/; //进位数
}
while(c)
{
r[j]=c%;
c=c/;
j++;
}
}
for(i=j-; i>=; i--)
printf("%d", r[i]);
printf("\n");
} int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
if(n== ||n==)
{
printf("1\n");
continue;
}
else
{
jiecheng(n);
}
}
return ;
}

顺便提一下,如何计算一个数n的阶乘结果的位数,这是数学方法,不理解的话记住就好!

所谓n!的十进制位数,就是 log(n)+1, 根据数学公式有:n!=1*2*3*.....*n;

lg(n!)=lg(2)+......lg(n);

代码:

#include <string>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <cmath> using namespace std; int main()
{
long int n;
long int i;
double sum; while(scanf("%ld", &n)!=EOF)
{
sum=0.0;
for(i=; i<=n; i++)
{
sum+=log10(i);
}
printf("%ld\n", (int)sum+ );
}
return ;
}

最新文章

  1. TCP/UDP简易通信框架源码,支持轻松管理多个TCP服务端(客户端)、UDP客户端
  2. PostScript的简单例子-用粗线画一个圆
  3. CentOS内核编译
  4. Linux网卡启动报错(this device is not active)
  5. java之四大皆空
  6. HDU 1160 FatMouse&#39;s Speed (sort + dp)
  7. Struts2 Tomcat的配置
  8. 谁知道哪有比较好的Beijing Milonga?想去参加这样的阿根廷探戈舞会~
  9. 一步一步学EF系列2【最简单的一个实例】
  10. 每天一个linux命令(25)--Linux文件属性详解
  11. qplot函数用法(转载)
  12. Java中enum的学习总结
  13. uvalive 3213 Ancient Cipher
  14. [Swift]LeetCode339. 嵌套链表权重和 $ Nested List Weight Sum
  15. Jenkins安装时Web页面报错提示离线安装
  16. 用Go语言实现一个简单的聊天机器人
  17. 关于Oxygen版 Eclipse JSP或html 中&lt;option&gt;标签嵌入jstl 如&lt;c:if&gt;出现报错
  18. js验证身份证类
  19. 【洛谷】P1641 [SCOI2010]生成字符串(思维+组合+逆元)
  20. iOS Sprite Kit教程之场景的设置

热门文章

  1. uva 10561 sg定理
  2. 花匠(codevs 3289)
  3. Biorhythms(poj 1006)
  4. hdu 5288 ZCC loves straight flush
  5. Android数据存储之Shared Preferences共享数据存储
  6. P1551 亲戚 洛谷
  7. jQuery学习总结(一)——jQuery基础与学习资源
  8. android EditText监听和长度监测事件
  9. BUPT复试专题—复数集合(?)
  10. [转]java类 对象 和构造方法