大数问题(相加) A + B
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的更多相关文章
- Java大数相加-hdu1047
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1047 题目描述: 题意有点绕,但是仔细的读了后就发现是处理大数相加的问题.注意:输入数据有多组,每组输 ...
- poj 2506 Tiling(递推 大数)
题目:http://poj.org/problem?id=2506 题解:f[n]=f[n-2]*2+f[n-1],主要是大数的相加; 以前做过了的 #include<stdio.h> # ...
- 《剑指offer》 大数递增
本题来自<剑指offer> 大数的存储 题目: 针对以下问题:大数的存储.大数的相加.大数的运算. 思路: 当数据较大时候,long long数据已经存储不了,借助数组的方式进行存储. 假 ...
- 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. ...
- hdu acm-1047 Integer Inquiry(大数相加)
Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 用字符串模拟两个大数相加——java实现
问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...
- 随机数组&大数相加
随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中 一, 设计思路: 先生成随机数数组,再将数组保存在一个字符串中,然后将数组各数字加和, ...
- java-两个大数相加
题目要求:用字符串模拟两个大数相加. 一.使用BigInteger类.BigDecimal类 public static void main(String[] args) { String a=&qu ...
- HDU 1018 大数(求N!的位数/相加)
Big Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- POJ 1503 Integer Inquiry(大数相加,java)
题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main ...
随机推荐
- .net中的目录
System.Environment.CurrentDirectory Application.StartupPath https://msdn.microsoft.com/en-us/library ...
- http://www.secrepo.com 安全相关的数据获取源
来自:http://www.secrepo.com Network MACCDC2012 - Generated with Bro from the 2012 dataset A nice datas ...
- Ext4,Ext3的特点和区别
Linux kernel 自 2.6.28 开始正式支持新的文件系统 Ext4. Ext4 是 Ext3 的改进版,修改了 Ext3 中部分重要的数据结构,而不仅仅像 Ext3 对 Ext2 那样,只 ...
- ionic2集成极光推送
ionic2集成极光推送: ionic2api:https://ionicframework.com/docs/ 极光推送官网:https://www.jiguang.cn android-怎么注册极 ...
- CreateProcess
#include <Windows.h> //WINBASEAPI //BOOL //WINAPI //CreateProcessW( //_In_opt_ LPCWSTR lpAppli ...
- ApacheFlink简介
对无界数据集的连续处理 在我们详细介绍Flink之前,让我们从更高的层面上回顾处理数据时可能遇到的数据集的类型以及您可以选择处理的执行模型的类型.这两个想法经常被混淆,清楚地区分它们是有用的. 首先, ...
- WPF学习(四) - 附加属性
冷静了一晚,我就当这次学习的过程是在看狗血剧情的武打小说吧:没有垃圾的武术,只有垃圾的武者…… 还有个话儿怎么说来着:你们是用户,不是客户,也就有个使用的权力.搞清楚身份,别叽叽歪歪的! 没办法,全世 ...
- P3809 【模版】后缀排序
题目背景 这是一道模版题. 题目描述 读入一个长度为 nn 的由大小写英文字母或数字组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在原串中的位置.位置编 ...
- MyBatis数据持久化(九)动态sql
本文摘自:mybatis参考文档中文版 MyBatis的一个强大的特性之一通常是它的动态SQL能力.如果你有使用JDBC或其他相似框架的经验,你就明白条件地串联SQL字符串在一起是多么的痛苦,确保不能 ...
- Android 自定义View 之利用ViewPager 实现画廊效果(滑动放大缩小)
http://www.2cto.com/kf/201608/542107.html