HDU 1002 A + B Problem II(大整数相加)

A + B Problem II

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

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 = 1111111111111111110 

题目大意:

大整数相加。

解题思路:

先把短的补齐。从最后一位開始计算。不进为就直接放进容器,进为把取余的放进容器,然后前一位加一。

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>

using namespace std;

int t;
string str1,str2;
vector <char> v;

void solve(){
    string temp;
    int a,l2;
    if(str1.length()<str2.length()){
        temp=str1;str1=str2;str2=temp;
        l2=str2.length();
    }
    for(int i=0;i<str1.length()-l2;i++){
        str2.insert(0,1,'0');
    }
    for(int i=0;i<str1.length();i++){
        a=str1[str1.length()-i-1]+str2[str2.length()-i-1]-2*'0';
        if(a>=10){
            v.push_back(a%10+'0');
            if(str1.length()-i-1==0){
                v.push_back('1');break;
            }
            str1[str1.length()-i-2]=(char)(str1[str1.length()-i-2]+1);
        }
        else v.push_back((char)(a+'0'));
    }
     vector<char>::iterator it=v.end();
     it--;
     while(it!=v.begin()){
        if(*it=='0')
            v.erase(it);
        else break;
        it--;
     }
    for(int i=v.size()-1;i>=0;i--){
       cout<<v[i];
    }
    cout<<endl;
}

int main(){
    int casen=0;
    scanf("%d",&t);
    while(t-->0){
        cin>>str1>>str2;
        printf("Case %d:\n%s + %s = ",++casen,str1.c_str(),str2.c_str());
        v.clear();
        solve();
        if(t!=0)
            cout<<endl;
    }
    return 0;
}
时间: 2024-10-12 15:47:40

HDU 1002 A + B Problem II(大整数相加)的相关文章

HDU 1002 A + B Problem II(两个大数相加)

详细题目点击:http://acm.hdu.edu.cn/showproblem.php?pid=1002 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)

抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)

数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. 1 while (a) //将每位数字取出来,取完为止 2 { 3 num1[i]=a%10; //将每一个各位取出存在数组里面,实现了将数字反转 4 i++; //数组的变化 5 a/=10; 6 } 趁热打铁 例题:hdu 4554 叛逆的小明 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid

hdu 1002.A + B Problem II 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目意思:就是大整数加法. 两年几前做的,纯粹是整理下来的. 1 #include <stdio.h> 2 #include <string.h> 3 4 #define max 1010 5 char a[max], b[max]; 6 7 int main() 8 { 9 int i, k, s, c, T, len1, len2; 10 scanf("%d&quo

HDU - 1002 A + B Problem II (大整数加法)

一道很基础的大整数加法. 简单的说一下思路吧. 先用字符串读取两个大数.首先需要把数组给初始化为0方便以后处理,然后对数组逆序对齐处理,接着相加转化后的两个数组并把之存进结果数组里面,最后对结果数组进行进位处理. 看代码吧. #include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #include <queue> #include <stac

hdu 1002 A + B Problem II(大正整数相加)

代码: #include<cstdio> #include<cstring> #define Min(a,b) ((a)<(b)?(a):(b)) using namespace std; char s1[10000],s2[10000],s3[10000]; int main() { int t; scanf("%d",&t); int tt=0; while(t--) { ++tt; if(tt!=1) printf("\n"

hdu 1002 A + B Problem II(大数模拟加法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目要求:输入t 为测试组数,然后输入两个正数 a,b求和: [思路] 首先,用字符串读入a,b,再将a,b 逆序存到数组中,然后对应位相加就行了,长度小的数要注意补零 倒过来做可以让进位变得方便 ,如果对应位的和大于等于10,就把余数储存在当前位置,后一位加1: [代码] #include <iostream> #include <string> #include <cs

hdu 1002 A + B Problem II(大数)

题意:整数大数加法 思路:大数模板 #include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> using namespace std; #define MAXN 9999//万进制 #define DLEN 4//4位 class BigNum{ private: int a[500];//可以控制大数位数(500*4) int len;//大数长度 public:

题解报告:hdu 1002 A + B Problem II(大数加法)

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 fol

杭电 1002 A + B Problem II【大数相加】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 解题思路:就是把大的数用数组存放起来,像小学的时候用竖式加法来算两个数相加那样算: 反思:思路很简单,可是有很多细节考虑不好,有时候没有进位,有时候又没有输出正确的答案,然后有时候数组长度又开小了什么的,所以还要多多练习. #include<stdio.h> #include<string.h> #define max 1000 void add(char a[],char b[