qq_58625182 2021-08-22 11:06 采纳率: 100%
浏览 106
已结题

这个求99以内阶乘的程序怎么错了?

#include <stdio.h>
#include <stdlib.h>
typedef struct{
char num[200];//大数
int count;大数的位数
}data;
data total(data temp,int num);
data elemulti(data temp,int elementx);
int main(int argc, char *argv[]) {
data temp={{'1'},1};
int i;
for(i=1;i<=24;i++)
temp=total(temp,i);
for(i=temp.count-1;i>=0;i--)
printf("%c",temp.num[i]);
return 0;
}
data elemulti(data temp,int elementx){
int i,t;
data tempx={{0}};
for(i=0;i<temp.count;i++){
t=(temp.num[i]-'0')*elementx;
tempx.num[i]+=(t%10+'0');
if(tempx.num[i]-'0'>=10){
tempx.num[i+1]+=1;
tempx.num[i]=(tempx.num[i]-'0')%10+'0';
}
tempx.num[i+1]+=t/10;
if(i==temp.count-1&&t/10>0){
temp.count++;
tempx.num[i+1]+='0';
break;
}
}
tempx.count=temp.count;
return tempx;
}
data total(data temp,int num){
data temp1,temp2;
int i,t=0;
int element1=num%10;//个位
int element2=(num/10)%10;//十位
if(element2==0)
temp=elemulti(temp,element1);
else{
temp1=elemulti(temp,element1);
temp2=elemulti(temp,element2);
temp.num[0]=temp1.num[0];
temp.count=temp2.count+1;
for(i=0;i<temp2.count;i++){//个位与十位相加
if(i<temp1.count-1){
temp.num[i+1]=t+temp2.num[i]+temp1.num[i+1]-'0';
t=0;
}
else{
temp.num[i+1]=temp2.num[i]+t;
t=0;
}
if(temp.num[i+1]-'0'>9){
t=((temp.num[i+1]-'0')/10);
temp.num[i+1]=(temp.num[i+1]-'0')%10+'0';
if(i==temp2.count-1){
temp.count++;
temp.num[i+2]=t+'0';
}
}
}
}
return temp;
}
23的阶乘都能算,24的阶乘最高位差了1。

  • 写回答

5条回答 默认 最新

  • zara 2021-08-23 21:38
    关注

    就当前位的计算来说是对的,但最后一位时判断的 t 没有加上啊;当然了,前面的也没加,但由于你说的 += 操作,所以,不影响。
    这两行修改如下试试,即不用 += 而是直接加到 t 里?

        t = ( temp.num[i] - '0' ) * elementx + tempx.num[i];
        tempx.num[i] = ( t % 10 + '0' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 9月1日
  • 已采纳回答 8月24日
  • 创建了问题 8月22日

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算