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. 采集电脑摄像头和mic,rtp端口推送音视频工具

    介绍:这个是我在做一个rtmp播放的项目中自己写的rtp推送的工具,可选择摄像头,可选择推送rtp的端口和ip 下载地址: github:https://github.com/alexhegang/s ...

  3. 创建ios界面的三步骤

    1.加载数据 (包括懒加载和字典转模型等) 2.搭建界面 (常见的有九宫格算法和for循环的嵌套等) 3.实现用户交互 (通常用按钮实现)

  4. Codeforces 676E The Last Fight Between Human and AI 规律

    链接 Codeforces 676E The Last Fight Between Human and AI 题意 给一个多项式,有些系数不确定.人和机器轮流填系数,系数可以是任何数,问是否能使得最后 ...

  5. etxjs

    序言 编辑 功能丰富,无人能出其右. 无论是界面之美,还是功能之强,ext的表格控件都高居榜首. 单选行,多选行,高亮显示选中的行,拖拽改变列宽度,按列排序,这些基本功能ExtJS轻量级实现. 自动生 ...

  6. 《鸟哥的Linux私房菜》笔记——02. 关于Linux

    Unix 历史 1969年以前:伟大的梦想--Bell, MIT 与 GE 的「Multics」系统 1969年:Ken Thompson 的小型 file server system 1973年:U ...

  7. Caffe学习--Blob分析

    Caffe_blob 1.基本数据结构 Blob为模板类,可以理解为四维数组,n * c * h * w的结构,Layer内为blob输入data和diff,Layer间的blob为学习的参数.内部封 ...

  8. SPA SEO SSR三者有什么区别

    SPA通俗的说就是单页面应用(single page application) 优点 页面之间的切换非常快 一定程度减少了后端服务器的压力 后端程序只需要提供api,不需要客户端到底是web端还是手机 ...

  9. 51nod 麦克打电话(AC自动机+树状数组)

    SAM+线段树合并的裸题. 但我们讨论AC自动机的做法. 先建出AC自动机.考虑询问在[a,b]中出现的次数就是\([1,b]\)的出现次数-\([1,a-1]\)的出现次数.把询问离线.然后我们要求 ...

  10. vue中使用viewerjs

    项目创建 插件Viewer.js vue init webpack mytest001 安装viewerjs npm install viewerjs 删掉生成的项目里面的helloWord.vue ...