HDU-1042 高精度

/*
 * hdu-1042 n!
 * mike-w
 * 2012-5-21
 * big number multiplies small number
 */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX_NUM_LEN 100000
#define WIDTH 4
#define BASE 10000

int mul1(int *s, int e)
{
	int end=s[0];
	int carry=0,i;
	for(i=MAX_NUM_LEN-1;i>=end;i--)
	{
		s[i]=s[i]*e+carry;
		carry=s[i]/BASE;
		s[i]%=BASE;
	}
	while(carry)
	{
		s[i]=carry;
		carry=s[i]/BASE;
		s[i]%=BASE;
		i--;
	}
	s[0]=i;
	return 0;
}

int disp(int *s)
{
	int i=s[0];
	while(i<MAX_NUM_LEN && s[i]==0)
		i++;
	if(i==MAX_NUM_LEN)
		putchar('0');
	else
		printf("%d",s[i]);
	for(i++;i<MAX_NUM_LEN;i++)
		printf("%0*d",WIDTH,s[i]);
	return 0;
}

int main(void)
{
	int n,i;
	int s[MAX_NUM_LEN];
	while(scanf("%d",&n)!=EOF)
	{
		memset(s,0,sizeof(int)*MAX_NUM_LEN);
		s[0]=MAX_NUM_LEN-1;
		s[MAX_NUM_LEN-1]=1;
		for(i=1;i<=n;i++)
			mul1(s,i);
		disp(s);
		putchar('\n');
	}
	return 0;
}
/*
 * extra words;
 * 加上了近似的起始位s[0]后,速度比以前快了不少!
 * 在学习中不断进步。
 */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值