本文主要是介绍大数阶层实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
// N!.cpp : 定义控制台应用程序的入口点。
///*
N!Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27897 Accepted Submission(s): 7646Problem 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 3Sample Output
1 2 6
*/#include "stdafx.h"//结果缓存长度
#define MAX_RES_LEN 10000int _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;
}
这篇关于大数阶层实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!