• HDU1047 Integer Inquiry(大数加法)


    题目链接

    分析:

    简单的大数加法。

    注意:

    1.处理结果前导0

    2.0000+000这样的情况

    AC代码如下:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define max(a, b) ((a)>(b)?(a):(b))
    
    void add_big(char *a, char *b, char *c){
        int len1 = strlen(a), len2 = strlen(b), len3;
        int i, j, k;
    
        len3 = max(len1, len2)+2;
        memset(c, 0, sizeof(char)*len3);
    
        i = len1-1; j = len2-1; k = len3 -1;
    
        while(i>=0 && j >= 0){
            c[k--] = a[i--] - '0' + b[j--] - '0';
        }
    
        while(i>=0) c[k--] = a[i--] - '0';
        while(j>=0) c[k--] = b[j--] - '0';
    
        for(i=len3-1; i>0; i--){
            c[i-1] += c[i] / 10;
            c[i] %= 10;
        }
    
        for(i=0; i<len3; i++) c[i] += '0';
    
        i=0; j=0;
    
        while(c[i] == '0' && c[i] != '\0')
            i++;
    
        if(c[i] == '\0'){
            c[0] = '0'; c[1] = '\0';
            return ;
        }
        else while(i<len3) c[j++] = c[i++];
    
        c[j] = '\0';
    }
    
    int main(){
        char a[1000], b[1000], c[1000];
        int T, i;
        scanf("%d", &T);
    
        for(i=0; i<T; i++){
            c[0] = '0'; c[1] = '\0';
            while(scanf("%s", a) == 1 && strcmp(a, "0") != 0){
                strcpy(b, c);
                add_big(a, b, c);
            }
            printf("%s\n", c);
            if(i != T-1) putchar('\n');
        }
    
        return 0;
    }
  • 相关阅读:
    Java多线程之监控Java线程池运行状态
    JS自学笔记02
    JS自学笔记01
    JAVA自学笔记09
    前端自学笔记07
    前端自学笔记06
    前端自学笔记05
    前端自学笔记04
    前端自学笔记03
    前端自学笔记02
  • 原文地址:https://www.cnblogs.com/tanhehe/p/2973433.html
Copyright © 2020-2023  润新知