首页 > 编程知识 正文

高精度乘法计算阶乘,一阶精度和二阶精度

时间:2023-05-04 21:40:20 阅读:242229 作者:2939

方法一:完全模拟竖式计算,没计算一位就处理相应的结果
方法二:先将每一位都乘以2,进行计算,等全都乘完以后在进行相应的处理
方法一的代码 :

#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>#include <math.h>#include <ctype.h>#include <map>#include <set>#include <vector>#include <queue>#define inf 0x3f3f3f3f#define eps 1e-8#define pi 3.1415typedef long long ll;using namespace std;int x[1000];int main(){ memset(x,0,sizeof(x)); int n; cin>>n; x[0]=1; int ans=1;//ans是位数 int t=0;//t类似于余数 for(int j=0; j<n; j++)//这是2的阶乘大小,即计算次数 { t=0;//每次都要初始化t为0 for(int i=0; i<ans; i++) { x[i]=x[i]*2+t; t=x[i]/10; x[i]=x[i]%10; } x[ans]+=t; if(t) ans++;//如果最后一次相乘t不为0,就说明位数又加1了 } for(int i=ans-1; i>=0; i--) printf("%d",x[i]);//最后逆序输出结果 printf("n"); return 0;}

方法二日后再补

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。