题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1042

题目大意:求n!, n 的上限是10000。

解题思路:高精度乘法 , 因为数据量比较大, 所以得用到缩进,但因为是乘法,缩进只能在10^5, 不然在乘的过程中就超过int型。然后数值位数大概在8000位以下。

#include <iostream>
#include <string.h>
using namespace std; const int MAXN = 100000;
const int N = 8000; struct bign {
int len;
int s[N]; bign() {
this -> len = 1;
memset(s, 0, sizeof(s));
}
bign (int number) {*this = number;}
bign (const char* number) {*this = number;} bign change(bign cur) {
bign now;
now = cur;
for (int i = 0; i < cur.len; i++)
now.s[i] = cur.s[cur.len - i - 1];
return now;
} void delZore() { // 删除前导0.
bign now = change(*this);
while (now.s[now.len - 1] == 0 && now.len > 1) {
now.len--;
}
*this = change(now);
} void put() { // 输出数值。
delZore();
printf("%d", s[0]);
for (int i = 1; i < len; i++)
printf("%05d", s[i]);
} bign operator = (const char *number) {
memset(s, 0, sizeof(s));
int dist = strlen(number);
int k = dist % 8;
for (int i = 0; i < k; i++)
s[0] = s[0] * 10 + number[i] - '0';
int cnt = 0;
for (int i = k; i < dist; i++, cnt++)
s[cnt / 8 + 1] = s[cnt / 8 + 1] * 10 + number[i] - '0';
len = cnt / 8 + 1;
return *this;
} bign operator = (int number) {
char string[N];
sprintf(string, "%d", number);
*this = string;
return *this;
} bign operator * (const bign &cur){
bign sum, a, b;
sum.len = 0;
a = a.change(*this);
b = b.change(cur); for (int i = 0; i < a.len; i++){
int g = 0; for (int j = 0; j < b.len; j++){
int x = a.s[i] * b.s[j] + g + sum.s[i + j];
sum.s[i + j] = x % MAXN;
g = x / MAXN;
}
sum.len = i + b.len; while (g){
sum.s[sum.len++] = g % MAXN;
g = g / MAXN;
}
}
return sum.change(sum);
}
}; int main() {
int n;
while (scanf("%d", &n) == 1) {
bign sum = 1;
for (int i = 2; i <= n; i++) {
bign now = i;
sum = sum * now;
}
sum.put();
printf("\n");
}
return 0;
}

hdu 1042 N!(高精度乘法 + 缩进)的更多相关文章

  1. hdu 1042 N!(高精度乘法)

    Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!   Input One N in ...

  2. hdu 1042 N! 高精度运算

    N!                                                                              Time Limit: 10000/50 ...

  3. Hdu 1042 N! (高精度数)

    Problem Description Givenan integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input OneN in one ...

  4. HDU 1042.N!【高精度乘法】【8月24】

    N! Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!   Input One N ...

  5. HDU 1042 N!( 高精度乘法水 )

    链接:传送门 思路:高精度乘法板子题,高精度耗时又耗空间...... /**************************************************************** ...

  6. [vijos P1040] 高精度乘法

    如果这次noip没考好,完全是因为从7月29日之后就没有再写过程序了.说起来,真是一个泪流满面的事实… 那这样一个弱智题练手恢复代码能力,竟然还花了我两个晚上(当然不是两整个晚上…) 第一天TLE了, ...

  7. 【PKU1001】Exponentiation(高精度乘法)

    Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 145642   Accepted: 35529 ...

  8. HDU 1042 N! 參考代码

    HDU 1042 N! 题意:给定整数N(0 ≤ N ≤ 10000), 求 N! (题目链接) #include <iostream> using namespace std; //每一 ...

  9. Vijos 1040 高精度乘法

    描述 高精度乘法 输入:两行,每行表示一个非负整数(不超过10000位) 输出:两数的乘积. 样例1 样例输入1 99 101 样例输出1 9999 题解 这道题和之前的Vijos 1010 清帝之惑 ...

随机推荐

  1. FAQs: 我们可以在那里来为我的没有提升管理权限的应用程序存储用户数据?

    如果你正在写一个不需要管理员权限的应用程序,如写一个业务线应用(Line of Business,LOB),用户应用程序如游戏,你总是要把应用程序的数据写到一个标准用可以访问的目录.下面列出一些所推荐 ...

  2. CentOS 6.7安装配置Cacti监控系统

    一.安装配置LAMP环境 yum -y install httpd php php-mysql php-snmp php-xml php-gd mysql mysql-server 启动http和my ...

  3. “System.Transactions.Diagnostics.DiagnosticTrace”的类型初始值设定项引发异常。

    今天在项目中用log4net,App.config文件中增加了configSections节点,程序运行报错“System.Transactions.Diagnostics.DiagnosticTra ...

  4. PGsql解决时差24H

    SELECT sa_ed_time, sa_st_time, case when sa_ed_time > sa_st_time then extract(EPOCH FROM (sa_ed_t ...

  5. 3. NHibernate基础知识 - 你必须知道的一些事情

    首先介绍一下框架结构(这个有个概念就可以): 然后我们会介绍一个很重要的概念(一定要好看)!! 这节对 NHibernate 架构做一个介绍,首先要了解一下该框架在应用程序中的位置: 先来一个简单的图 ...

  6. python基础知识五

    数据结构基本上就是---它们可以处理一些数据的结构.或者说,它们是用来存储一组相关数据的. python中有三种内建的数据结构---列表.元祖和字典. 我们将会学习如何使用它们,以及它们如何使编程变得 ...

  7. Oracle学习第二天

    oracle数据库的常见数据类型oracle全部数据类型 有26种 char定长字符串类型 长度是固定不变的 例如:no char(10) 如果存入的值不足十个字符,其它位也被占用默认长度是1 最大长 ...

  8. 【html】【3】html标签列表

    必看参考: http://www.divcss5.com/html/h323.shtml http://www.w3school.com.cn/tags/tag_html.asp 常用: <ht ...

  9. Android运行异常情况分析(持续更新)

    1.java.lang.IllegalAccessException: access to class not allowed 原因:在写class 文件的时候没有把class设置成public 2. ...

  10. Ubuntu系列Crontab日记记录

    修改rsyslog文件,将/etc/rsyslog.d/50-default.conf 文件中的#cron.*前的#删掉 重启rsyslog服务service rsyslog restart 重启cr ...