大数阶层实现

// N!.cpp : 定义控制台应用程序的入口点。
//

/*
N!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27897    Accepted Submission(s): 7646

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

Input
One N in one line, process to the end of file.

Output
For each N, output N! in one line.

Sample Input
1 2 3

Sample Output
1 2 6
*/

#include "stdafx.h"

//结果缓存长度
#define MAX_RES_LEN 10000

int _tmain(int argc, _TCHAR* argv[])
{
	int n;	//输入的阶层数
	int s;	//单个结果与n相乘后的临时变量;


	char res[MAX_RES_LEN];	//结果

	int i;
	while(scanf("%d",&n)!=EOF)
	{
		int carry = 0;		//进位;
		int residue = 0;	//余数
		res[MAX_RES_LEN - 1] = 1;
		int lenRes = 1;		//结果的长度

		while (n-- >= 2)
		{
			for (i = 1; i <= lenRes; i++)
			{
				s = res[MAX_RES_LEN - i] * n + carry;
				carry = s / 10;
				residue = s % 10;
				res[MAX_RES_LEN - i]= (char) residue;
			}

			while (carry != 0)
			{
				residue = carry % 10;
				carry = carry / 10;
				i++;
				if (i > MAX_RES_LEN)
				{
					printf ("error!\n");
					exit(0);
				}
				res[MAX_RES_LEN - i]= (char) residue;
				
			}
			lenRes = i - 1;
		}

		for (i = lenRes; i >= 1; i--)
		{
			printf ("%c", res[MAX_RES_LEN - i] + '0');
		}

		printf ("\n");
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值