6-10 阶乘计算升级版 (20point(s)).c

本题要求实现一个打印非负整数阶乘的函数。

函数接口定义:

void Print_Factorial ( const int N );

其中N是用户传入的参数,其值不超过1000。如果N是非负整数,则该函数必须在一行中打印出N!的值,否则打印“Invalid input”。

裁判测试程序样例:

#include <stdio.h>

void Print_Factorial ( const int N );

int main()
{
    int N;
	
    scanf("%d", &N);
    Print_Factorial(N);
    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

15

输出样例:

1307674368000
/* 你的代码将被嵌在这里 */
void Print_Factorial ( const int N )
{
	long long sum=1;
	if(N==0)
		printf("%lld",1);
	if(N>0)
	{
		int a[100000]={0};   //数据过大只能用数组存放
		int weishu=1;
		int num=0,out,in,i;
		a[0]=1;
		for(out=2;out<=N;out++)  //最外层循环表示输入的数字
		{
			for(in=0;in<weishu;in++) //位数
			{
				int temp=a[in]*out+num;
				a[in]=temp%10;     //不断求位数和进制上的数字
				num=temp/10;
			}
			while(num!=0)
			{
				a[weishu]=num%10;
				weishu++;
				num=num/10;
			}
		}
		for(i=weishu-1;i>=0;i--)
			printf("%d",a[i]);
	}
	if(N<0)
		printf("Invalid input");
}

 

发布了131 篇原创文章 · 获赞 94 · 访问量 2945

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/105127143