文章

36

粉丝

0

获赞

4

访问

10.3k

头像
大整数加法 题解:C语言只有50%
P1474 武汉大学2018年机试题
发布于2024年3月28日 14:33
阅读数 199

#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

int main(){
    char s1[1000],s2[1000],r[2000];
    while(scanf("%s %s",s1,s2)!=EOF){
        int len = strlen(s1);
        int temp=0,count=0,plus=0;
        for(int i=len-1;i>=0;i--){
            if(i==0&&((s1[i]-'0')+(s2[i]-'0')>=10)){
                temp = (s1[i]-'0')+(s2[i]-'0');
                r[count++] = temp%10+plus+'0';
                plus = temp / 10; //15+15
                r[count++] = plus+'0';
            }
            else{
                temp = (s1[i]-'0')+(s2[i]-'0');
                r[count++] = temp%10+plus+'0';
                plus = temp / 10; //15+15
            }
           
        }
        for(int i=count-1;i>=0;i--) printf("%c",r[i]);
        printf("\n");
    }
    return 0;
}



 

登录查看完整内容


登录后发布评论

1 条评论
snake
2024年3月29日 16:25

plus的位置加错了

temp = (s1[i]-'0')+(s2[i]-'0')+plus;

赞(0)