Fork me on GitHub

题目1198:a+b(高精度计算,好像有点问题)

题目链接:http://ac.jobdu.com/problem.php?pid=1198

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

参考代码:

//
//  1198 a+b.cpp
//  Jobdu
//
//  Created by PengFei_Zheng on 16/04/2017.
//  Copyright © 2017 PengFei_Zheng. All rights reserved.
//
 
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
 
using namespace std;
 
char a[1010], b[1010], c[1010];
 
int main(){
    while(scanf("%s %s",a,b)!=EOF){
        int lena = (int)strlen(a);
        int lenb = (int)strlen(b);
        int i, j;
        int carry, len, tmp;
        carry = len = tmp = 0;
         
        for(i = lena-1, j = lenb-1; i >= 0 && j >= 0; i--, j--){
            tmp = a[i]-'0' + b[j]-'0' +carry;
            c[len++] = tmp%10;
            carry = tmp/10;
        }
         
        while(i >= 0){
            tmp = carry + a[i] - '0';
            c[len++] = tmp%10;
            carry = tmp/10;
            i--;
        }
         
        while(j >= 0 ){
            tmp = carry + b[j] - '0';
            c[len++] = tmp%10;
            carry = tmp/10;
            j--;
        }
         
        if(carry!=0){
            c[len++] = carry;
        }
         
        len--;
        for(; len>=0; len--){
            printf("%d",c[len]);
        }
        printf("\n");
    }
    return 0;
}
/**************************************************************
    Problem: 1198
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:130 ms
    Memory:1520 kb
****************************************************************/

 

posted @ 2017-04-16 14:23  伊甸一点  阅读(295)  评论(0编辑  收藏  举报