A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 407964    Accepted Submission(s): 79058


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,比如:000+1=001 。

看着别人的代码,顺着思路改了一下现在的输出是没有前面的0的,即:000+1=1 。

这两种写法都能AC,不去掉0的输出时比较简单。

还有就是注意输出的时候除最后一组数之外,每两组数之间有一个空行

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
const int maxn=1e6+10;
int a[maxn],b[maxn],c[maxn];
char num1[maxn],num2[maxn];
int main()
{
int t;
int k=1;
int i;
scanf("%d",&t);
while(t--)
{
int sum;
int flag=0;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
scanf("%s %s",num1,num2);
int l1=strlen(num1);
int l2=strlen(num2);
for(i=0;i<l1;i++) a[i]=num1[l1-1-i]-'0';
for(i=0;i<l2;i++) b[i]=num2[l2-1-i]-'0';//字符串转换成数字
int ml=std::max(l1,l2);//找到最长的字符串长度
for(sum=0,i=0;i<ml;i++)
{
c[i]=(a[i]+b[i]+sum)%10;
sum=(a[i]+b[i]+sum)/10;
}
if(sum) c[ml]=1;
printf("Case %d:\n",k++);
printf("%s + %s = ",num1,num2);
if(c[ml]==1) printf("1");
for(i=ml-1;i>=0;i--)
{
if(c[i]!=0)
{
flag++;
printf("%d",c[i]);
}
else if(c[i]==0&&flag!=0)
{
printf("0");
flag++;
}
else if(c[i]==0&&flag==0) continue;
}
if(c[0]==0&&flag==0) printf("0");
/*这种写法是不去掉开头的0的
for(i=ml-1;i>=0;i--) printf("%d",c[i]);*/
printf("\n");
if(t) printf("\n");
}
return 0;
}

HDU 1002:A + B Problem II(大数相加)的更多相关文章

  1. 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)

    数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...

  2. HDU 1002 A + B Problem II(高精度加法(C++/Java))

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

  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. HDU 1002 A + B Problem II(大整数相加)

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

  5. HDU 1002 A + B Problem II

    A + B Problem II   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted ...

  6. 大数加法~HDU 1002 A + B Problem II

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 题意: 数学题,A+B; 思路,这个数非常大,普通加法一定会超时,所以用大数加法.大数加法的基 ...

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

    题目链接 Problem Description I have a very simple problem for you. Given two integers A and B, your job ...

  8. hdu 1002 A + B Problem II【大数加法】

    题目链接>>>>>> 题目大意:手动模拟大数加法,从而进行两个大数的加法运算 #include <stdio.h> #include <strin ...

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

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

  10. hdu 1002 A + B Problem II(大正整数相加)

    代码: #include<cstdio> #include<cstring> #define Min(a,b) ((a)<(b)?(a):(b)) using names ...

随机推荐

  1. PHP算法之排序算法(PHP内置排序函数)

    首先用实例来讲述一下PHP内置的一些排序函数 [a / k] sort [/ rsort]:[保留索引关系 / 按键名(保留键名关系,适用于关联数组)] 对数组进行排序,结束时数组单元将被从最低到最高 ...

  2. Solaris 11, gcc 的安装

    注意点在于, 头文件在另外一个包system/header里,需要另外安装 pkg pkg install system/header

  3. 广播 (Broadcast)

    广播 :在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制.我们拿广播电台来做个比方.我们平常使用收音机收音是这样的:许许多多不同的广播电台通过特定的频率来发送他们的内 ...

  4. P3226 [HNOI2012]集合选数

    考虑构造矩阵 1 3 9 27...... 2 6 18 54...... 4 12 36 108...... ...... 发现在这个矩阵上一个合法的集合是一个满足选择的数字不相邻的集合,由于行数列 ...

  5. Ivan and Burgers CodeForces - 1100F (线性基)

    大意: 给定n元素序列, m个询问$(l,r)$, 求$[l,r]$中选出任意数异或后的最大值 线性基沙茶题, 直接线段树暴力维护两个log还是能过的 #include <iostream> ...

  6. Codeword CodeForces - 666C (字符串计数)

    链接 大意:求只含小写字母, 长度为n, 且可以与给定模板串匹配的字符串个数 (多组数据) 记模板串为P, 长为x, 总串为S. 设$f_i$为S为i时的匹配数, 考虑P最后一位的首次匹配位置. 若为 ...

  7. UVA-10026 Shoemaker's Problem (贪心)

    题目大意:一个鞋匠,有n只鞋要修,修某只鞋的时间ti已知,某只鞋晚修一天要交的罚款fi也已知.现在让找个修鞋顺序使得罚款最少. 题目分析:本来想水一下这道题,没想到真的AC啦.后来又查的题解,找的解释 ...

  8. php中点击网页不跳转执行程序

    if($code['result_code'] !='FAIL') { echo "<script type='text/javascript'> alert('退款成功,请耐心 ...

  9. duilib 实现列表头任意拖动

    1.表头(xml) <List name="List_records" padding="5,10,5,5" bkcolor="#FFFFFFF ...

  10. PHP:第一章——php中数据类型和强制类型转换

    <?php //PHP中的数据类型: //标量类型:布尔型(boolean).整型(integer).浮点型(float).字符串型(string) //复合类型:数组(array).对象(ob ...