大数加法,A+B
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 169591 Accepted Submission(s): 32528
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.
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.
1 2
112233445566778899 998877665544332211
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
用字符数组保存大数,模拟手算注意进位处理,以及第一位数是否进位。
#include<stdio.h>
#include<string.h>
#define N 1001
int main()
{
char str1[N],str2[N];
int t,i = 0;
scanf("%d",&t);
while(t--)
{
i++;
scanf("%s %s",str1,str2);
printf("Case %d:\n%s + %s = ",i,str1,str2);
int a[N]={0},b[N]={0},k,i;
k = strlen(str1)>strlen(str2)?strlen(str1):strlen(str2);
for(i = 0;i<strlen(str1);i++)
a[i] = str1[strlen(str1)-1-i]-'0';
for(i = 0;i<strlen(str2);i++)
b[i] = str2[strlen(str2)-1-i]-'0';
if(strlen(str1)>strlen(str2))
{
for(i = 0;i<strlen(str1);i++)
{
a[i]+=b[i];
if(a[i]>9)
{
a[i]-=10;
a[i+1]++;
}
}
if(a[k]!=0)
for(i = k;i>=0;i--)
printf("%d",a[i]);
else
for(i = k-1;i>=0;i--)
printf("%d",a[i]);
}
else
{
for(i = 0;i<strlen(str2);i++)
{
b[i]+=a[i];
if(b[i]>9)
{
b[i]-=10;
b[i+1]++;
}
}
if(b[k]!=0)
for(i = k;i>=0;i--)
printf("%d",b[i]);
else
for(i = k-1;i>=0;i--)
printf("%d",b[i]);
}
printf("\n");
if(t)
printf("\n");
}
return 0;
}
大数加法,A+B的更多相关文章
- 51nod 1005 大数加法
#include<iostream> #include<string> using namespace std; #define MAXN 10001 },b[MAXN]={} ...
- c#大数加法
在C#中,我们经常需要表示整数.但是,c#的基本数据类型中,最大的long也只能表示-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807之间的数 ...
- 玲珑杯1007-A 八进制大数加法(实现逻辑陷阱与题目套路)
题目连接:http://www.ifrog.cc/acm/problem/1056 DESCRIPTION Two octal number integers a, b are given, and ...
- Leetcode 67 Add Binary 大数加法+字符串处理
题意:两个二进制数相加,大数加法的变形 大数加法流程: 1.倒置两个大数,这一步能使所有大数对齐 2.逐位相加,同时进位 3.倒置两个大数的和作为输出 class Solution { public: ...
- HDU1002大数加法
大数加法 c++版: #include <map> #include <set> #include <stack> #include <queue> # ...
- java实现大数加法、乘法(BigDecimal)
之前写过用vector.string实现大数加法,现在用java的BigDecimal类,代码简单很多.但是在online-judge上,java的代码运行时间和内存大得多. java大数加法:求a+ ...
- vector、string实现大数加法乘法
理解 vector 是一个容器,是一个数据集,里边装了很多个元素.与数组最大的不同是 vector 可以动态增长. 用 vector 实现大数运算的关键是,以 string 的方式读入一个大数,然后将 ...
- 【大数加法】POJ-1503、NYOJ-103
1503:Integer Inquiry 总时间限制: 1000ms 内存限制: 65536kB 描述 One of the first users of BIT's new supercompu ...
- A + B Problem II 大数加法
题目描述: Input The first line of the input contains an integer T(1<=T<=20) which means the number ...
- NI笔试——大数加法
NI笔试: 1.找出字符串第一次出现的字符.用数组建立哈希表,然后再扫描字符串并判断次数是否为1. 2.大数加法,即字符串加法.因为之前写过乘法,就以为是乘法.然后就把乘法写上去了····= = 好了 ...
随机推荐
- HDOJ(1005) Number Sequence
这道题,咋一看很像Fibonacci数列,使用递归或者改进的动态规划来解决.但是仔细一看,(1 <= n <= 100,000,000),使用这些方法,要么超时,要么肯定内存不够用,因此必 ...
- 基于bootstrap3的 表格和分页的插件
如题 样式呢就是bootstrap3 的 功能呢就是实现表格和分页 (以上废话) 本来是自己没事儿写的一个js插件,曾经搁浅了一阵子,但最近由于公司项目的原因也需要这样的一个插件,所以就捡起来做了个可 ...
- css3动画--超级基础知识
这么乱的笔记....供自己阅读!
- 多线程Demo
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- off() 方法 与 unbind() 方法移除绑定事件的处理程序。one()函数用于为每个匹配元素的一个或多个事件绑定一次性事件处理函数
off() 方法移除用.on()绑定的事件处理程序. unbind() 方法移除用.bind()绑定的事件处理程序. 从 jQuery 1.7开始, .on() 和 .off()方法是最好的元素上附加 ...
- osg 基本几何图元
转自:osg 基本几何图元 //osg 基本几何图元 // ogs中所有加入场景中的数据都会加入到一个Group类对象中,几何图元作为一个对象由osg::Geode类来组织管理. // 绘制几何图元对 ...
- MSSql ID自动增长删除数据重1开始
dbcc checkident('db_Tome1.dbo.员工信息表',reseed,0) 注:dbcc checkident('表名',reseed,0)
- 2014年度辛星html教程夏季版第七节
经过前面六节的学习,我们大致清楚了HTML教程中的基础内容,那么接下来我们开始继续向后推进,可以说,下面我们介绍一下HTML中的区块. ***************区块*************** ...
- 大话string
最近看完大话string之后深有感悟,虽然写c#不知不觉的已经写了四年了,但是很多原理也一直不太明白,最近看完这个才算略微明白了一些. string类型有2个重要的特性,一致性和驻留性. 一致性就是说 ...
- DOS下文件操作命令
文件名是由文件路径和文件名称合起来的,如C:\DOS\COMMAND.COM. DIR 显示文件和文件夹(目录). 用法:DIR [文件名] [选项] 它有很多选项,如/A表示显示所有文件(即包括带隐 ...