HDU 1002 A + B Problem II(大整数相加)[通俗易懂]

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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115967.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux之awk用法详解

    linux之awk用法详解[转自:http://blog.chinaunix.net/uid-23302288-id-3785105.html]awk是行处理器:相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常用来格式化文本信息awk处理过程: 依次对每一行进行处理,然后输出awk命令形式:awk[-F|-f|-v]‘BEGIN{}//{command1;comman…

    2022年7月27日
    15
  • Determining IP information for eth0… failed; no link present.  Check cable?

    Determining IP information for eth0… failed; no link present.  Check cable?问题1:docker pull nginx 拉取失败问题2:Determining IP information for eth0… failed; no link present. Check cable?问题3:“VMware Network Adapter VMnet8”没有有效的 IP 配置问题4:没有开启VMware NAT service和VMware DHCP …

    2022年6月13日
    35
  • int和int32的区别_int float double char区别

    int和int32的区别_int float double char区别Java中没有Int32,Int64,,只有int,short,longJava中int就代表Int32,short就代表Int16,long就代表Int64首先,几个基本的关键字:Int16=short,占2个字节.-32768~32767Int32=int,占4个字节.-2147483648~2147483647Int64=long,占8个字…

    2022年8月15日
    5
  • leapftp乱码_如何用网格本做笔记

    leapftp乱码_如何用网格本做笔记生活对我下了手2019年7月23星期二大晴天1.主要掌握怎么连接服务器2.单个文件上传3.整个文件夹上传leapftp界面主要功能板块介绍1.管理ftp服务器配置的地方2.服务器网站文件窗口界面3.上传状态的窗口界面4.正在上传的文件窗口界面5.本地电脑文件窗口界面怎么连接ftp服务器服务器上要有ftp服务,1.你要有ftp服务器的账号,2.你要有ftp服务器的密…

    2025年7月10日
    2
  • IDEA使用教程(一)_idea第一次使用教程

    IDEA使用教程(一)_idea第一次使用教程讲一下:1、CreateNewProject这个就是我们创建新项目的地方,一般出在第一次安装或者没有项目时出现这个。2、ImportProject这个就是引入工程,可以支持eclipse开发的项目(注意修改配置文件,坑很深)以及Maven项目。具体引入流程会在后面的博客中具体涉及。3、Open打开现有项目,别的IDE开发的,拿过来打开4、CheckoutfromVers…

    2022年10月13日
    2
  • pycharm帮助文档_pycharm pytorch

    pycharm帮助文档_pycharm pytorch大家在利用python进行机器学习时,pycharm是一个很不错的IDE。通过这段时间的使用,自己总结了一些使用心得,故试着写下来共勉,不当之处,希望正在阅读的你批评指正。1、pycharm的安装与激活pycharm安装可以根据自己需要在官网上下载,链接http://www.jetbrains.com/pycharm/download/#section=windows。修改h…

    2022年8月28日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号