HDOJ 1753 大明A+B

~~~题目链接~~~




#include <stdio.h>
#include <string.h>
int main()
{
    int i = 0, j = 0, k = 0, p1 = 0, p2 = 0, n = 0, c = 0, len1 = 0, len2 = 0;
    int l = 0, r = 0;
    int num1[402], num2[402], num3[402], num4[402];
    char str1[402], str2[402];
    while(scanf("%s %s", str1, str2) != EOF)
    {
        memset(num1, 0, sizeof(num1));
        memset(num2, 0, sizeof(num2));
        memset(num3, 0, sizeof(num3));
        memset(num4, 0, sizeof(num4));
        len1 = strlen(str1);
        len2 = strlen(str2);
        if(strchr(str1, '.') == NULL)
            p1 = len1;
        else
            p1 = strchr(str1, '.')-str1;
        if(strchr(str2, '.') == NULL)
            p2 = len2;
        else
        p2 = strchr(str2, '.')-str2;
        k = 0;
        for(i = p1+1; i<len1; i++)
            num1[k++] = str1[i]-'0';
        k = 0;
        for(i = p2+1; i<len2; i++)
            num2[k++] = str2[i]-'0';
        c = 0;
        for(i = 400; i>-1; i--)//求小数点后面的和
        {
            c = num1[i]+num2[i]+c;
            num3[i] = c%10;
            c = c/10;
        }
        l = r = 0;
        if(p1>p2)//对齐补0
            for(i = 0; i<p1-p2; i++)
                num2[r++] = 0;
        else
            for(i = 0; i<p2-p1; i++)
                num1[l++] = 0;
        for(i = 0; i<p1; i++)
            num1[l++] = str1[i]-'0';
        for(i = 0; i<p2; i++)
            num2[r++] = str2[i]-'0';
        if(p1>p2)
        {
            p1 = p1+p2;
            p2 = p1-p2;
            p1 = p1-p2;
        }
        k = 0;
        for(i = p2-1; i>-1; i--)//求小数点前面的和
        {
            c = c+num1[i]+num2[i];
            num4[i] = c%10;
            c = c/10;
        }
        if(c)
            printf("%d", c);
        k = 0;
        while(!c && num4[k] == 0 && k<p2) k++;//去前0
        for(i = k; i<p2; i++)
            printf("%d", num4[i]);
        if(k == p2)
            printf("0");
        k = 400;
        while(num3[k] == 0 && k>-1) k--;//去后0
        if(k != -1)
            printf(".");
        for(i = 0; i<=k; i++)
            printf("%d", num3[i]);
        memset(str1, 0, sizeof(str1));
        memset(str2, 0, sizeof(str2));
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值