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

AC代码:

#include<iostream>
#include<string.h>
using namespace std;
#include<stdio.h>
#define MAX 1010
int add1[MAX], add2[MAX], res[MAX];
char tmp1[MAX], tmp2[MAX];
int main()
{
int N, i, j, len, len1, len2, tmp, k;
scanf("%d",&N);
getchar();
for(j=;j<N;j++)
{
memset(add1,,sizeof(add1));
memset(add2,,sizeof(add2));
memset(res,,sizeof(res));
memset(tmp1,,sizeof(tmp1));
memset(tmp2,,sizeof(tmp2));
scanf("%s %s",tmp1,tmp2);
len1 = strlen(tmp1);
len2 = strlen(tmp2);
for(i=len1-,k=;i>=;i--)
add1[k++] = tmp1[i] - '0'; for(i=len2-,k=;i>=;i--)
add2[k++] = tmp2[i] - '0';
tmp = ;
if(len1 >= len2)
{
for(i=;i<=len1;i++)
{
res[i] = (add1[i] + add2[i] +tmp)%;
tmp = (add1[i] + add2[i] +tmp)/;
}
}
else if(len1 < len2)
{
for(i=;i<=len2;i++)
{
res[i] = (add1[i] + add2[i] +tmp)%;
tmp = (add1[i] + add2[i] +tmp)/;
}
}
if(len1 >= len2) len = len1;
else len = len2;
printf("Case %d:\n%s + %s = ",j+, tmp1 , tmp2);
if(res[len]!=) printf("%d",res[len]);
for(i=len-;i>=;i--)
printf("%d",res[i]); printf("\n");
if(j!=N-) printf("\n");
}
return ;
}

my AC:

#include<iostream>
#include<stdio.h>
#include<string.h>
#define N 1010
using namespace std;
char a[N],b[N];
int c[N],d[N],ans[N]; int main(){
int t ;
cin>>t;
getchar();
for(int i=;i<=t;i++)
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(c,,sizeof(c));
memset(d,,sizeof(d));
memset(ans,,sizeof(ans));
cin>>a>>b;
int la=strlen(a);
int lb=strlen(b);
for(int i=;i<la;i++) c[i]=a[la--i]-'';
for(int i=;i<lb;i++) d[i]=b[lb--i]-'';
int lans;
(la>lb)?lans=la:lans=lb;
int temp=;
for(int i=;i<=lans;i++){
ans[i]=(c[i]+d[i]+temp)%;
temp=(c[i]+d[i]+temp)/;
}
cout<<"Case "<<i<<":"<<endl;
cout<<a<<" + "<<b<<" = ";
for(int i=lans-;i>=;i--)
cout<<ans[i];
cout<<endl;
if(i!=t)cout<<endl;
} }
#include<iostream>
#include<stdio.h>
#include<string.h>
#define N 1010
using namespace std;
char a[N],b[N];
int c[N],d[N],ans[N]; int main(){
int t ;
cin>>t;
getchar();
for(int i=;i<=t;i++)
{
fill(a,a+N,);
fill(b,b+N,);
fill(c,c+N,);
fill(d,d+N,);
fill(ans,ans+N,);
cin>>a>>b;
int la=strlen(a);
int lb=strlen(b);
for(int i=;i<la;i++) c[i]=a[la--i]-'';
for(int i=;i<lb;i++) d[i]=b[lb--i]-'';
int lans;
(la>lb)?lans=la:lans=lb;
int temp=;
for(int i=;i<=lans;i++){
ans[i]=(c[i]+d[i]+temp)%;
temp=(c[i]+d[i]+temp)/;
}
cout<<"Case "<<i<<":"<<endl;
cout<<a<<" + "<<b<<" = ";
for(int i=lans-;i>=;i--)
cout<<ans[i];
cout<<endl;
if(i!=t)cout<<endl;
} }
#include<iostream>
#include<string.h>
using namespace std;
#include<stdio.h>
#define MAX 1010
int add1[MAX], add2[MAX], res[MAX];
char tmp1[MAX], tmp2[MAX];
int main()
{
int N, i, j, len, len1, len2, tmp, k;
scanf("%d",&N);
getchar();
for(j=;j<N;j++)
{
memset(add1,,sizeof(add1)) ; ||用0填充
memset(add2,,sizeof(add2));
memset(res,,sizeof(res));
memset(tmp1,,sizeof(tmp1));
memset(tmp2,,sizeof(tmp2)); tips:函数解释 void *memset(void *s, int ch, size_t n); 函数解释:将s中前n个字节替换为ch并返回s; cin>>tmp1>>tmp2; 量 数 组 长 度:
len1 = strlen(tmp1);
len2 = strlen(tmp2);
for(i=len1-,k=;i>=;i--)||改类型并换顺序
add1[k++] = tmp1[i] - ''; for(i=len2-,k=;i>=;i--)
add2[k++] = tmp2[i] - '';
tmp = ;
if(len1 >= len2)
{
for(i=;i<=len1;i++)
{
res[i] = (add1[i] + add2[i] +tmp)%;
tmp = (add1[i] + add2[i] +tmp)/;
}
}
else if(len1 < len2)
{
for(i=;i<=len2;i++)
{ 进位处理方法:
res[i] = (add1[i] + add2[i] +tmp)%;
tmp = (add1[i] + add2[i] +tmp)/;
}
}
if(len1 >= len2) len = len1;
else len = len2;
printf("Case %d:\n%s + %s = ",j+, tmp1 , tmp2);
if(res[len]!=) printf("%d",res[len]);
for(i=len-;i>=;i--)
printf("%d",res[i]); printf("\n");
if(j!=N-) printf("\n");
}
return ;
}

char tmp1[MAX], tmp2[MAX];

cin>>tmp1>>tmp2;

一长串的字符数组,还可以直接用cin进行输入

2. getchar()必须加

3.字符型与整数类型互换

for(i=len1-1,k=0;i>=0;i--)
add1[k++] = tmp1[i] - '0';

for(i=len2-1,k=0;i>=0;i--)
add2[k++] = tmp2[i] - '0';

其实呢-48也是一样的;

4.错误代码

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
int t;
long sa,sb;
cin>>t;
getchar();
for(int i=;i<=t;i++)
{
char a[],b[];
for(int j=;;j++)
{
char word=getchar();
if(word>=''&&word<='')
a[j]=word;
else
{
sa=j;
break;
}
}
for(int k=;;k++)
{
char word=getchar();
if(word>=''&&word<='')
b[k]=word;
else if(word=='\n')
{
sb=k;
break;
}
}
cout<<"Case "<<i<<":"<<endl;
for(int m=;m<sa;m++)
{
cout<<a[m];
}
cout<<" + ";
for(int n=;n<sb;n++)
{
cout<<b[n];
}
cout<<" = "; int c[]={};编译有警告
int sc,next,shu,shua,shub;
if(sa>sb)
sc=sa;
else
sc=sb; 出错数据举例, ,所给出结果数组的长度设置有问题,应该分情况讨论,有0单独分情况,并设置不显示
for(int o=sc-;o>=;o--,sa--,sb--)
{
if(sa>)
shua=a[sa-]-;
else
shua=if(sb>) shub=b[sb-]-;
else
shub=;
shu=shua+shub;
if(shu>=)
{
c[o-]=;
shu=shu-;
c[o]+=shu; 进位不全面, ,数据出错,所以不适宜顺序相加机制,应该采用逆序相加,顺序输出的方式
}
else
} for(int p=;p<sc;p++)
cout<<c[p];
cout<<endl;
if(i!=t)
cout<<endl;
}
#include<iostream>

大数问题(相加) A + B的更多相关文章

  1. Java大数相加-hdu1047

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1047 题目描述: 题意有点绕,但是仔细的读了后就发现是处理大数相加的问题.注意:输入数据有多组,每组输 ...

  2. poj 2506 Tiling(递推 大数)

    题目:http://poj.org/problem?id=2506 题解:f[n]=f[n-2]*2+f[n-1],主要是大数的相加; 以前做过了的 #include<stdio.h> # ...

  3. 《剑指offer》 大数递增

    本题来自<剑指offer> 大数的存储 题目: 针对以下问题:大数的存储.大数的相加.大数的运算. 思路: 当数据较大时候,long long数据已经存储不了,借助数组的方式进行存储. 假 ...

  4. Add to Array-Form of Integer LT989

    For a non-negative integer X, the array-form of X is an array of its digits in left to right order.  ...

  5. hdu acm-1047 Integer Inquiry(大数相加)

    Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. 用字符串模拟两个大数相加——java实现

    问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...

  7. 随机数组&大数相加

    随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中 一,      设计思路: 先生成随机数数组,再将数组保存在一个字符串中,然后将数组各数字加和, ...

  8. java-两个大数相加

    题目要求:用字符串模拟两个大数相加. 一.使用BigInteger类.BigDecimal类 public static void main(String[] args) { String a=&qu ...

  9. HDU 1018 大数(求N!的位数/相加)

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  10. POJ 1503 Integer Inquiry(大数相加,java)

    题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main ...

随机推荐

  1. 关于linux下QIODevice类进行读取的几个方法的理解

    Qt中对读写设备的支持力度很大,其都继承与QIODevice类,其中有几个方法是非常值得注意的,不管是在用原始的serial port进行通信还是使用网络的TCP/IP 或者UDP或者HTTP等协议时 ...

  2. luogu 2679 子串

    子串 史上最简短的一篇博客,毕竟看题解ac心疼我的kmp /* f[i][j][k][0/1]表示A的前i个,B的前j个,用到了k个子串,当前字符选或者不选. 所以f[0][0][0][0]的方案数为 ...

  3. MyBatis数据持久化(八)sql复用

    在mybatis中,我们可以將sql语句中公共的部分提取出来,然后需要该段sql的地方通过include标签引入即可,这样可以达到sql语句复用的目的. 例如我们有两条相似的查询语句: <sel ...

  4. Android 去掉TabLayout下的阴影,AppBarLayout下的阴影

    开始还以为是TabLayout在高版本系统上的特殊表现呢,没有在意,UI提出说感觉不好看就查了一下,原来是在TabLayout放在AppBarLayout里面才有这样的效果,只需要对AppBarLay ...

  5. 如何在Windows下安装Linux子系统(Ubuntu,openSUSU,SUSU Linux Server)

    注意:只有win10才能安装,安装的linux没有图形界面. 1.首先在win10设置 --> 更新与安装 --> 针对开发人员 ,选择开发人员模式. 2.win10 Cortana -- ...

  6. SpringMVC学习(二)——SpringMVC架构及组件(及其运行原理)-转载

    相信大家通过前文的学习,已经对SpringMVC这个框架多少有些理解了.还记得上一篇文章中SpringMVC的处理流程吗?  这个图大致描述了SpringMVC的整个处理流程,这个流程图还是相对来说比 ...

  7. 74HC165应用

    管脚定义与内部逻辑图 注1:其中控制管脚有3个:SH/LD-QH-CLK,CLK INH硬件接VSS,SER和QH'悬空 原理:先拉低SH/LD,A-H置入芯片内部寄存器中,然后拉高SH/LD,锁住A ...

  8. [SCOI2009]windy数 数位dp

    Code: #include<cmath> #include<iostream> #include<cstdio> using namespace std; con ...

  9. centos7 安装freeswitch

    1.安装运行库 yum install -y git gcc-c++ wget alsa-lib-devel autoconf automake bison broadvoice-devel bzip ...

  10. 再来一波PHP程序员必看书籍

    前言 https://segmentfault.com/a/11... 内列出的是已看过的. 本篇文章内列出的书籍是准备要看或者正在看的,与大家分享. 知识无价,还是建议各位童鞋把更多的资金投入到学习 ...