加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数相加算法

发布时间:2020-12-14 04:14:00 所属栏目:大数据 来源:网络整理
导读:/* author: openxmpp@163.com 由于GCC,VS的内置数据类型的加法受限于int,long,unsigned long等的精度,则对于大数的加法应采用字符串的形式进行输入和计算。 Problem Description I have a very simple problem for you. Given two integers A and B, your j

/*

author: openxmpp@163.com
由于GCC,VS的内置数据类型的加法受限于int,long,unsigned long等的精度,则对于大数的加法应采用字符串的形式进行输入和计算。
Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
 
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
 
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
 
Sample Input
2
1 2
112233445566778899 998877665544332211
 
Sample Output
Case 1:
1 + 2 = 3
 
Case 2:
112233445566778899 + 998877665544332211 = 2222222222222221110
*/
?
//排版好看的版本
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

#define ARRAY_SIZE 1024

void reverseInPlace( char str[] )
{
    int strLen = strlen(str);
    char *start = str;
    char *end = str + strLen - 1;
    while ( start < end )
    {
        char tmp = *start;
        *start = *end;
        *end = tmp;
        start ++;
        end -- ;
    }
}

void sum( char left[ARRAY_SIZE],char right[ARRAY_SIZE],char result[ARRAY_SIZE])
{
    memset(result,'',sizeof(result));
    reverseInPlace(left);
    reverseInPlace(right);
    int leftLen = strlen(left);
    int rightLen = strlen(right);
    bool biggerThan10 = false;
    int i = 0;
    for ( ; i < leftLen || i < rightLen ; i ++ )
    {
        char leftTmp = (left[i])?(left[i]):('0'); //如果加数和被加数的位数不同,则位数少的那个加完后其余地方的填充位为0,而不是期望的'0'(对应的ASCII码为48)
        char rightTmp = (right[i])?(right[i]):('0');
        int sum = 0;
        int tmpValue = (leftTmp - '0') + (rightTmp - '0');
        sum = (true == biggerThan10)?(tmpValue+1):(tmpValue);
        biggerThan10 = (sum>=10)?(true):(false);
        result[i] = sum % 10 + '0'; //当前的进位值为取余计算
    }
    if ( true == biggerThan10 )
    {
        result[i] = '1';
    }
    reverseInPlace(left); //翻转回去,方便打印
    reverseInPlace(right);
    reverseInPlace(result); //由于是从个位开始计算,输出的时候习惯个位在最右面,需要翻转
    return ;
}

int main(int argc,char* argv[])
{
    int grpNumber;
    cin >> grpNumber;
    char left[1024];
    char right[1024];
    char result[1024] = {0};
    for ( int i = 1 ; i <= grpNumber ; i ++ )
    {
        memset(left,1024);
        memset(right,1024);
        memset(result,1024);
        scanf("%s %s",left,right);
        sum(left,right,result);
        cout << "Case " << i <<":"<<endl;
        printf ("%s + %s = %sn",result);
        if ( i != grpNumber )
        {
            cout << endl; //坑爹的ACM判定系统,当处理最后一个输出时,不需要加'n'
        }
    }
    return 0;
}

?

下面是排版可以拷贝直接测试的版本

?

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
 
#define ARRAY_SIZE 1024
 
void reverseInPlace( char str[] )
{
    int strLen = strlen(str);
    char *start = str;
    char *end = str + strLen - 1;
    while ( start < end )
    {
        char tmp = *start;
        *start = *end;
        *end = tmp;
        start ++;
        end -- ;
    }
}
 
void sum( char left[ARRAY_SIZE],char right[ARRAY_SIZE],char result[ARRAY_SIZE])
{
    memset(result,'',sizeof(result));
    reverseInPlace(left);
    reverseInPlace(right);
    int leftLen = strlen(left);
    int rightLen = strlen(right);
    bool biggerThan10 = false;
    int i = 0;
    for ( ; i < leftLen || i < rightLen ; i ++ )
    {
        char leftTmp = (left[i])?(left[i]):('0'); //如果加数和被加数的位数不同,则位数少的那个加完后其余地方的填充位为0,而不是期望的'0'(对应的ASCII码为48
        char rightTmp = (right[i])?(right[i]):('0');
        int sum = 0;
        int tmpValue = (leftTmp - '0') + (rightTmp - '0');
        sum = (true == biggerThan10)?(tmpValue+1):(tmpValue);
        biggerThan10 = (sum>=10)?(true):(false);
        result[i] = sum % 10 + '0'; //当前的进位值为取余计算
    }
    if ( true == biggerThan10 )
    {
        result[i] = '1';
    }
    reverseInPlace(left); //翻转回去,方便打印
    reverseInPlace(right);
    reverseInPlace(result); //由于是从个位开始计算,输出的时候习惯个位在最右面,需要翻转
    return ;
}
 
int main(int argc, char* argv[])
{
    int grpNumber;
    cin >> grpNumber;
    char left[1024];
    char right[1024];
    char result[1024] = {0};
    for ( int i = 1 ; i <= grpNumber ; i ++ )
    {
        memset(left,1024);
        memset(right,1024);
        memset(result,1024);
        scanf("%s %s",left,right);
        sum(left,right,result);
        cout << "Case " << i <<":"<<endl;
        printf ("%s + %s = %sn",result);
        if ( i != grpNumber )
        {
            cout << endl; //坑爹的ACM判定系统,当处理最后一个输出时,不需要加'n'
        }
    }
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读