hdu1002 (大整数相加)

#include <stdio.h>
#include <string.h>

char *sum(char *a,char *b);

void main (){
    int n;
    char a[1005],b[1005];
    
    scanf("%d",&n);

    for(int i = 1;i < n;i++){
        scanf("%s%s",a,b);sum(a,b);
        printf("Case %d:\n",i);
        printf("%s + %s = %s\n\n",a,b,sum(a,b));
    }

    scanf("%s%s",a,b);sum(a,b);
    printf("Case %d:\n",n);
    printf("%s + %s = %s\n",a,b,sum(a,b));
}

char *sum(char *a,char *b){
    static char c[1005],*psum;memset(c,'\0',sizeof(c));
    int bit = 1003,tag = 0;
    int alen = strlen(a);
    int blen = strlen(b);
    int i = alen - 1,j = blen - 1;

    while(j >= 0 && i >= 0){    
        c[bit] = (a[i] + b[j] + tag - 96) % 10 + 48;
        tag = (a[i] + b[j] + tag - 96) / 10;
        bit--;i--;j--;
    }
    while(i >= 0){
        c[bit] = (a[i] + tag - 48) % 10 + 48;
        tag = (a[i] + tag - 48) / 10;
        i--;bit--;
    }
    while (j >= 0){
        c[bit] = (b[j] + tag - 48) % 10 + 48;    
        tag = (b[j] + tag - 48) / 10;
        j--;bit--;
    }
    if (tag != 0)    c[bit] = tag + 48;
    else c[bit] = '\0';

    for(i = 0;;i++){
        if (c[i] != '\0'){
            psum = &c[i];
            break;
        }
    }

    
    return (psum);

}

你可能感兴趣的:(c,include)