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 ...
随机推荐
- css 怎么在文字两边加上横线
<div class="title"> <h2 class="title-text"><span>工作经历</span ...
- Java后端进阶-网络编程(NIO/BIO)
Socket编程 BIO网络编程 BIO Server package com.study.hc.net.bio; import java.io.BufferedReader; import java ...
- 【笔记】《Redis设计与实现》chapter14 服务器
14.1 命令请求的执行过程 读取命令请求 命令执行器(1):查找命令实现 在命令表(command table)中查找参数所指定的命令,并将找到的命令保存到客户端状态的cmd属性里面 命令执行器(2 ...
- IDEA通过Maven打包JavaFX工程(OpenJFX11)
1 概述 最近研究JFX,写出来了但是打包不了,这...尴尬... IDEA的文档说只支持Java8打成jar包: 尝试过直接使用Maven插件的package,不行,也尝试过Build Artifa ...
- 12.Quick QML-QML 布局(Row、Column、Grid、Flow和嵌套布局) 、Repeater对象
1.Row布局 Row中的item可以不需要使用anchors布局,就能通过行的形式进行布局. 并且item可以使用Positioner附加属性来访问有关其在Row中的位置及其他信息. 示例如下所示, ...
- Ananagrams UVA - 156
Most crossword puzzle fans are used to anagrams - groups of words with the same letters in differe ...
- Django中的CBV视图
Web 开发是一项无聊而且单调的工作,特别是在视图功能编写方面更为显著.为了减少这种痛苦,Django植入了视图类这一功能,该功能封装了视图开发常用的代码,无须编写大量代码即可快速完成数据视图的开发, ...
- Nest 中处理 XML 类型的请求与响应
公众号及小程序的微信接口是通过 xml 格式进行数据交换的. 比如接收普通消息的接口: 当普通微信用户向公众账号发消息时,微信服务器将 POST 消息的 XML 数据包到开发者填写的 URL 上. - ...
- SpringMVC请求参数解析
请求参数解析 客户端请求在handlerMapping中找到对应handler后,将会继续执行DispatchServlet的doPatch()方法. 首先是找到handler对应的适配器. Hand ...
- 逻辑引擎、工作流、CMDB小感
工作流是啥? 在界面上画画点点就能生成代码,这是很吸引人的事情,也是很多自动化工具追求的目标.工作流就是这么一个东西,通过定义流程和输入,就能实现你想要的东西,不需要编写代码. 工作流的实现 通过解析 ...