UVA OJ 623 500!
| 500! |
In these days you can more and more often happen to see programs which perform some useful calculations being executed rather then trivial screen savers. Some of them check the system message queue and in case of finding it empty (for examples somebody is editing a file and stays idle for some time) execute its own algorithm.
As an examples we can give programs which calculate primary numbers.
One can also imagine a program which calculates a factorial of given numbers. In this case it is the time complexity of order O(n) which makes troubles, but the memory requirements. Considering the fact that 500! gives 1135-digit number no standard, neither integer nor floating, data type is applicable here.
Your task is to write a programs which calculates a factorial of a given number.
Assumptions: Value of a number ``n" which factorial should be calculated of does not exceed 1000 (although 500! is the name of the problem, 500! is a small limit).
Input
Any number of lines, each containing value ``n" for which you should provide value of n!
Output
2 lines for each input case. First should contain value ``n" followed by character `!'. The second should contain calculated value n!.
Sample Input
10
30
50
100
Sample Output
10!
3628800
30!
265252859812191058636308480000000
50!
30414093201713378043612608166064768844377641568960512000000000000
100!
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
算法竞赛入门经典上的解法如下:会超时。
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 3000;
int a[maxn];
int main()
{
int n;
int i, j;
int c, s;
while (scanf("%d", &n) == 1)
{ memset(a,0,sizeof(a));
a[0] = 1;
for (i = 2; i <= n; i++)
{
c = 0;
for (j = 0; j < maxn; j++)
{
s = a[j] * i + c;
a[j] = s % 10;
c = s/10;
}
}
for (i = maxn; i >= 0;i--)
if (a[i])
break;
printf("%d!\n",n);
for (j = i; j >= 0; j--)
printf("%d",a[j]);
printf("\n");
} return 0;
}
修改后如下:
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 500;
int a[maxn];
int main()
{
int n;
int i, j;
int len;
int c, s;
while (scanf("%d", &n) == 1)
{ memset(a, 0, sizeof(a));
a[0] = 1;
len = 1;
for (i = 2; i <= n; i++)
{
c = 0;
for (j = 0; j < len; j++)
{
s = a[j] * i + c;
a[j] = s % 1000000;
c = s / 1000000;
}
if (c)
a[len++] = c;
}
printf("%d!\n", n);
printf("%d",a[len-1]);
for (j = len-2; j >= 0; j--)
printf("%06d", a[j]);
printf("\n");
} return 0;
}
UVA OJ 623 500!的更多相关文章
- uva oj 567 - Risk(Floyd算法)
/* 一张有20个顶点的图上. 依次输入每个点与哪些点直接相连. 并且多次询问两点间,最短需要经过几条路才能从一点到达另一点. bfs 水过 */ #include<iostream> # ...
- UVa OJ 194 - Triangle (三角形)
Time limit: 30.000 seconds限时30.000秒 Problem问题 A triangle is a basic shape of planar geometry. It con ...
- UVa OJ 175 - Keywords (关键字)
Time limit: 3.000 seconds限时3.000秒 Problem问题 Many researchers are faced with an ever increasing numbe ...
- UVa OJ 197 - Cube (立方体)
Time limit: 30.000 seconds限时30.000秒 Problem问题 There was once a 3 by 3 by 3 cube built of 27 smaller ...
- UVa OJ 180 - Eeny Meeny
Time limit: 3.000 seconds限时3.000秒 Problem问题 In darkest <name of continent/island deleted to preve ...
- UVa OJ 140 - Bandwidth (带宽)
Time limit: 3.000 seconds限时3.000秒 Problem问题 Given a graph (V,E) where V is a set of nodes and E is a ...
- 548 - Tree (UVa OJ)
Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...
- UVa OJ 10300
Problem A Ecological Premium Input: standard input Output: standard output Time Limit: 1 second Memo ...
- UVa OJ 10071
Problem B Back to High School Physics Input: standard input Output: standard output A particle has i ...
随机推荐
- ApiTesting全链路接口自动化测试框架 - 实战应用
场景一.添加公共配置 我们在做自动化开始的时候,一般有很多公共的环境配置,比如host.token.user等等,如果这些放在用例中,一旦修改,将非常的不便.麻烦(尤其切换环境). 所以这里我们提供了 ...
- 冒泡排序(BubbleSort)
介绍: 冒泡排序是一种最基础的交换排序(两两比较待排序的关键字,交换不满足次序要求的那对数,直到整个表都满足次序要求为止),工作方式如同碳酸饮料中的二氧化碳气泡最终会上浮到顶端一样,故名"冒 ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- js--如何实现继承?
前言 学习过 java 的同学应该都知道,常见的继承有接口继承和实现继承,接口继承只需要继承父类的方法签名,实现继承则继承父类的实际的方法,js 中主要依靠原型链来实现继承,无法做接口继承. 学习 j ...
- day-02-循环
while 循环 why:大气循环, 吃饭,上课,睡觉,日复一日,歌曲列表循序环,程序中:输入用户名密码, what:while 无限循环. how: 基本结构: while 条件: 循环体 初识循环 ...
- Java后端进阶-消息中间件
1.分布式事务 MQ分布式消息中间件实战应用 通过控制台就可以定义 二.分布式消息中间件 解决耦合的问题 http内容很繁琐,而且是短连接响应后就会中断 持久化 消息分发 高可用 高可靠
- 使用Vue-TreeSelect组件实现公司-部门-人员级联下拉列表的处理
最近在改造原有Bootstrap开发框架,增加一个Vue&Element前端的时候,发现需要处理一个级联更新的过程,就是选择公司,然后更新部门,选择部门,或者人员列表,选择作为主管的一个实现, ...
- 使用Leaflet创建地图模块
背景 最近需要为某单位开发地图展示系统,因此开始涉略和使用Leaflet这个轻量级地图库. 创建基础地图需要以下几步 引入相关js和css文件,创建基础地图 <div id="map& ...
- 大学四年,总结一套适合小白的Java自学路线和方法
前言篇 大家好,我是bigsai 好久不见,甚是想念,文章同时收录在回车课堂(文底阅读原文可达). 无论你是大学生还是在职人员,想学Java时,都会面临两个选择,自学或者报班.报班通常太费钱,时间又不 ...
- PBR(基于物理的渲染)学习笔记2
相关资料 https://www.cnblogs.com/dojo-lzz/p/13237686.html 文档:PBR学习笔记.note 链接:http://note.youdao.com/note ...