题解 | #高精度整数加法#

高精度整数加法

http://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6

#include "stdio.h"
#include "string.h"
int main()
{
    char buf1[10001],buf2[10001];
    while(scanf("%s",buf1)!=EOF)
    {
        char count[10002]={'\0'};
        scanf("%s",buf2);
        int len1 = strlen(buf1);
        int len2 = strlen(buf2);
        int jinwei=0,max =0,min=0;
        char *max_buf,*min_buf;
        if(len1>len2) 
        {
            max = len1;
            max_buf = buf1;
            min = len2;
            min_buf = buf2;
        }
        else 
        {
            max = len2;
            max_buf = buf2;
            min = len1;
            min_buf = buf1;
        }
        for(int i=0;i<max+1;i++)
        {
            if(min-1-i>=0)
            {
                int temp=0;
                temp = max_buf[max-1-i] -'0' + min_buf[min-1-i]-'0'+ jinwei;
                jinwei = temp/10;
                count[max-i] = '0'+ temp%10;
            }
            else if(max-1-i>=0)
            {
                int temp=0;
                temp = max_buf[max-1-i]-'0'+ jinwei;
                jinwei = temp/10;
                count[max-i] = '0'+ temp%10;
            }
            else
            {
                count[max-i] = '0'+ jinwei;
            }
        }
        if(count[0]=='0')printf("%s\n",count+1);
        else printf("%s\n",count);
    }
    return 0;
}

好像考虑多了 但是又没有多

全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务