大数阶层实现

来源:互联网 发布:java开发规范 编辑:程序博客网 时间:2024/05/17 02:38
// 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 DescriptionGiven an integer N(0 ≤ N ≤ 10000), your task is to calculate N!InputOne N in one line, process to the end of file.OutputFor each N, output N! in one line.Sample Input1 2 3Sample Output1 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;}


原创粉丝点击