Time Limit: 3 second

Memory Limit: 2 MB

对于阶乘函数,即使自变量较小,其函数值也会相当大。例如: 10!=3628800 25!=15511210043330985984000000 若用integer型数据表示阶乘,最多仅可用7!,用longint类型类型亦只能到12!。设计一个程序,当键入一个正整数n(1<=n<=100)时,输出n!的精确值。

如果N的值不在规定的范围,将输出“error”。

Input

输入文件中只一个数字,表示要计算的n的值(1<=n<=100)。

Output

输出N!的精确值

Sample Input

12

Sample Output

12!=479001600

【题解】

高精度*单精度。乘的时候要记录进位信息。然后处理进位即可。不断更新数字的长度。最后倒叙输出

【代码】

#include <cstdio>
#include <stdlib.h> const int MAXN = 500; int n,a[MAXN],la = 1; void input_data() //输入数据
{
scanf("%d",&n);
if ( n<1 || n>100) //输出错误信息
{
printf("error");
exit(0);
}
for (int i = 2; i <= MAXN;i++)
a[i] = 0;
a[1] = 1; //先置1方便后面乘
} void get_ans()
{
for (int i = 1;i <= n;i++) //从1 一直 乘到n
{
int x = 0;
for (int j = 1;j <= la;j++) //对现在的数字的每一位都乘上i x用来保存进位信息
{
a[j] = a[j] * i + x;
x = a[j] / 10;
a[j] = a[j] % 10;
}
while (x > 0) //如果进位数字>0则 数字的位数可以增加 不断增加即可
{
la++;
a[la] += x;
x = a[la] / 10;
a[la] = a[la] %10;
}
}
} void output_ans()
{
printf("%d!=",n);
for (int i = la;i >= 1;i--)
printf("%d",a[i]); } int main()
{
input_data();
get_ans();
output_ans();
return 0;
}

【2006】求N!的精确值的更多相关文章

  1. HDU 2006 求奇数的乘积

    http://acm.hdu.edu.cn/showproblem.php?pid=2006 Problem Description 给你n个整数,求他们中所有奇数的乘积.   Input 输入数据包 ...

  2. HDOJ 2006 求奇数的乘积

    #include<iostream> #include<vector> using namespace std; int main() { int n; while (cin ...

  3. 【9101】求n!的值

    Time Limit: 10 second Memory Limit: 2 MB 问题描述 用高精度的方法,求n!的精确值(n的值以一般整数输入). Input 文件输入仅一行,输入n. Output ...

  4. poj 3134 Power Calculus(迭代加深dfs+强剪枝)

    Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multipli ...

  5. 杭电oj2000-2011

    2000  ASCII码排序 #include <stdio.h> int main(){ char a,b,c,t; while(scanf("%c%c%c", &a ...

  6. 瘋子C++笔记

    瘋耔C++笔记 欢迎关注瘋耔新浪微博:http://weibo.com/cpjphone 参考:C++程序设计(谭浩强) 参考:http://c.biancheng.net/cpp/biancheng ...

  7. 2006 ACM Northwestern European Programming Contest C题(二分求最大)

    My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a numberN o ...

  8. 【9112】求2的n次方的精确值

    Time Limit: 1 second Memory Limit: 2 MB 问题描述 求2^n的精确值.n由用户输入,0<=n<=3232. Input 输入只有一行,一个正整数n. ...

  9. 2006 ACM 求奇数的和

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2006 注意 sum=1,写在while 不然每次结果会累积 #include <stdio.h> ...

随机推荐

  1. UVA 10306 e-Coins(全然背包: 二维限制条件)

    UVA 10306 e-Coins(全然背包: 二维限制条件) option=com_onlinejudge&Itemid=8&page=show_problem&proble ...

  2. WIN8.1的安装和打开"这台电脑"速度很慢的解决办法

    WIN8.1的安装和打开"这台电脑"速度很慢的解决办法 对于非服务器用的电脑,如果电脑的内存在2G或更高,首推的操作系统是 WINDOWS8.1 64位企业版,用了就知道,没有比这流畅懂事的操作系统. ...

  3. js35

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  4. Linux下基于LDAP统一用户认证的研究

    Linux下基于LDAP统一用户认证的研究                   本文出自 "李晨光原创技术博客" 博客,谢绝转载!

  5. Android Okhttp完美同步持久Cookie实现免登录

    通过对Retrofit2.0的<Retrofit 2.0 超能实践,完美支持Https传输>基础入门和案例实践,掌握了怎么样使用Retrofit访问网络,加入自定义header,包括加入S ...

  6. 《二》Java IO 流的分类介绍

    一.根据流向分为输入流和输出流: 注意输入流和输出流是相对于程序而言的. 输出:把程序(内存)中的内容输出到磁盘.光盘等存储设备中        输入:读取外部数据(磁盘.光盘等存储设备的数据)到程序 ...

  7. vue中类名和组件经过刷新不对应的解决办法

    方法一: 页面路由如下: index.js路由文件如下: { path: '/myOrder', name: '我的订单', menuShow: true, component: myOrder, c ...

  8. sql server try catch

    一直不习惯Sql Server 2000提供的错误处理机制----繁琐,别扭...如今,Sql Server 2005提供了对Try...Catch的支持,我们总算可以象写程序一样写SQL语句了:) ...

  9. POJ——T2271 Guardian of Decency

    http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   A ...

  10. Microsoft SQL Server Version List(SQL Server 版本)

    原帖地址 What version of SQL Server do I have? This unofficial build chart lists all of the known Servic ...