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 = 1111111111111111110
 
思路:字符串输入,将两个字符串倒叙输入两个新整型数组(一开始一直在纠结如何倒叙相加)。然后相加进位。注意最后要判断进位是否为0 .
 
注意!!!一定要将两个新的整型数组初始化为0(这长度不一的两个数倒叙相加时不会出错),另外最好用一个新的数组来接受相加结果。。
 
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std; int main()
{
int n , ans = ;
cin >> n ;
while(n --)
{
ans++ ;
char a[] = {}, b[] ={} ;
int c[] ={}, d[] = {} , e[] ={};
scanf("%s%s" , &a , &b);
int len1 = strlen(a) ;
int len2 = strlen(b) ;
int len = max(len1 , len2);
int j = ;
for(int i = len1 - ; i >= ; i--)
{
c[j++] = a[i] - ;
}
j = ;
for(int i = len2 - ; i >= ; i--)
{
d[j++] = b[i] - ;
}
int k = ;
for(int i = ; i < len ; i++)
{
e[i] = (c[i] + d[i] + k) % ;
k = (c[i] + d[i] + k) / ;
}
cout << "Case " << ans << ':' << endl ;
printf("%s + %s = " , a , b);
if(!k)
{
for(int i = len - ; i >= ; i--)
cout << e[i] ;
cout << endl ;
}
else
{
e[len] = k ;
for(int i = len ; i >= ; i--)
cout << e[i] ;
cout << endl ;
}
if(n != )
printf("\n"); }
return ;
}
 

A + B Problem II(1002)的更多相关文章

  1. nyoj 623 A*B Problem II(矩阵)

    A*B Problem II 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 ACM的C++同学有好多作业要做,最头痛莫过于线性代数了,因为每次做到矩阵相乘的时候,大 ...

  2. hdu 1002 A + B Problem II(大数)

    题意:就是求a+b (a,b都不超过1000位) 思路:用数组存储 第一道大数的题目,虽然很水,纪念一下! 代码: #include<cstdio> #include<cstring ...

  3. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

  4. 【BZOJ2998】Problem A(动态规划)

    [BZOJ2998]Problem A(动态规划) 题面 BZOJ 题解 一个人的成绩范围可以确定为一个区间 这样就变成了 选择若干区间,不重合, 每个区间有个权值,求最大权值和 这样就可直接\(dp ...

  5. 【Luogu1414】又是毕业季II(数论)

    [Luogu1414]又是毕业季II(数论) 题面 题目背景 "叮铃铃铃",随着高考最后一科结考铃声的敲响,三年青春时光顿时凝固于此刻.毕业的欣喜怎敌那离别的不舍,憧憬着未来仍毋忘 ...

  6. 【BZOJ2302】[HAOI2011]Problem C(动态规划)

    [BZOJ2302][HAOI2011]Problem C(动态规划) 题面 BZOJ 洛谷 题解 首先如果\(m=0\)即没有特殊限制的话,那么就和这道题目基本上是一样的. 然而这题也有属于这题的性 ...

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

    A + B Problem II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

  8. HDU 1002:A + B Problem II(大数相加)

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. A + B Problem II(杭电1002)

    /*A + B Problem II Problem Description I have a very simple problem for you. Given two integers A an ...

随机推荐

  1. 配置apache密码认证

    配置apache密码认证     apache提供了一系列的认证,授权,访问控制模块,我们这里选用最方便的mod_auth_basic,mod_authn_file,mod_authz_user这三个 ...

  2. 【LuoguP3747】[六省联考2017] 相逢是问候

    题目链接 题意 给定一个长度为 n 的序列 a , 给定一个正整数 c 每次修改操作是把一段区间内的数 \(x_i\) 修改为 \(c^{x_i}\) 询问区间和模 p 的结果 Sol 修改是把一个数 ...

  3. day5 函数

      1.求全部元素的和 [1,2,1,2,3,3,3,3] 遍历 a = [1,2,1,2,3,3,3,3] sum = 0 n = len(a)-1 while n>=0: sum += a[ ...

  4. Java日期时间以及日期相互转换_java - JAVA

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 Java日期时间,以及相互转化,供大家参考,具体内容如下 package com.study.string; impor ...

  5. unittest----skip(跳过用例)

    我们在执行测试用例时,有时有些用例时不需要执行的,这时就需要用到unittest给我们提供的跳过用例的方法 @unittest.skip(reason):强制跳过,不需要判断条件.reason是跳过原 ...

  6. python学习相关软件安装

    爬虫初学者必装的软件!! 一.安装python step1:安装包下载 1.1.官网下载地址:https://www.python.org/ 根据自己电脑系统选择,我选择windowns 有不同版本可 ...

  7. js数字每3位加一个逗号

    if(typeof val ==="number"){ var str = val.toString(); ? /(\d)(?=(\d{})+\.)/g : /(\d)(?=(?: ...

  8. 给字体和元素加阴影text-shadow和box-shadow

    1.语法:  对象选择器 {text-shadow:X轴偏移量 Y轴偏移量 阴影模糊半径 阴影颜色} 注:text-shadow可以使用一个或多个投影,如果使用多个投影时必须需要用逗号“,”分开. 2 ...

  9. 170826-关于spring的知识点及练习

    1.Spring作用: 1.生态体系庞大,全能型选手![springmvc是其一个子模块,jdbcTemplate能直接操作数据库!] 2.将其他组件粘合在一起 3.IOC容器和AOP[Aspect ...

  10. tp5关联模型进行条件查询

    public function wordOne(){ return $this->hasOne('TeachWord','id','w_id')->field('id,pid,title' ...