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