4-10 阶乘计算升级版 (20分)

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

函数接口定义:

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 i,s=1;
	if(N>=0&&N<=12){
		for(i=2 ;i<=N ;i++){
			s *= i;
		}
		printf("%ld\n",s);
	}
	else if(N>12&&N<=1000){
		int num[3000] = {0};
		num[0] = 1;
		int k=1;  //位数
		int n=0;  //进位
		int temp;
		for(int i=2 ;i<=N ;i++){
			for(int j=0;j<k;j++){
				temp = num[j]*i+n;  //每一位相乘 再+进位
				num[j] = temp%10;	//更新每一位的数字
				n = temp/10;		//判断能否进位
			}	
			while(n!=0){  //如果可以进位
				num[k] = n%10;  //新增一位
				n /=10;  //继续判断能否进位
				k++;
			}
		}
		for(int x=k-1;x>=0;x--){		//输出数字
			printf("%d",num[x]);
		}
	}
	else{
		printf("%s\n","Invalid input");
	}
}


  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值