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. response.sendRedirect跳转 jsp:forward跳转

    response.sendRedirect跳转 <% response.sendRedirect("online.jsp"); %> jsp:forward跳转 < ...

  2. C/C++中的实参和形参,重点以及盲点,自己以前未知的

    C/C++中的实参和形参   今天突然看到一道关于形参和实参的题,我居然不求甚解.藐视过去在我的脑海里只有一个参数的概念,对于形参和实参的区别还真的不知道,作为学习了几年C++的人来说,真的深深感觉对 ...

  3. vuecli3 引入script 针对没有cmd amd require等方式的js

    最近做高德开发,需要引入高德的js,但是 说实话 高德官方的文档不知道大佬们有没有看懂,反正我是没看懂,写的都什么鬼?我怎么引都引入不了,迫不得已想到了如下方法: 一.准备一个能够在页面中插入js的方 ...

  4. Syntax error: Bad for loop variable解决办法

    从 ubuntu 6.10 开始,ubuntu 就将先前默认的bash shell 更换成了dash shell:其表现为 /bin/sh 链接倒了/bin/dash而不是传统的/bin/bash. ...

  5. IOS UI-Button按钮

    #import "ViewController.h" @interface ViewController () { UILabel *lable; BOOL moveFlag; N ...

  6. innodb_trx, innodb_locks, innodb_lock_waits

    如果两个事务出现相互等待,则会导致死锁,MySQL的innodb_lock_wait_timeout参数设置了等待的时间限制,超时则抛异常. select @@innodb_lock_wait_tim ...

  7. echatrs可视化图在隐藏后显示不出来或是宽度出现问题

    最近在做一个可视化的项目,用了百度的ECharts.js作为可视化的视图框架,echarts的实例很多,基本能满足项目的需求,而且文档也相对完整.清晰,是个很不错的前端可视化框架. 我们的项目是使用b ...

  8. 进程控制fork vfork,父子进程,vfork保证子进程先运行

    主要函数: fork 用于创建一个新进程 exit 用于终止进程 exec 用于执行一个程序 wait 将父进程挂起,等待子进程结束 getpid 获取当前进程的进程ID nice 改变进程的优先级 ...

  9. 框架:Lucene.net

    Lucene.net 性能<第八篇> 摘要: 一.IndexReader性能分析 IndexReader完成了打开所有索引文件和提供底层reader API等繁重的工作,而IndexSea ...

  10. Microsoft Jet 数据库引擎 SQL 和 ANSI SQL 的比较

    http://westlife063.blog.163.com/blog/static/129942096201052591210384/   Microsoft Jet 数据库引擎 SQL 和 AN ...