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 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式