大数加法,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.大数加法,即字符串加法.因为之前写过乘法,就以为是乘法.然后就把乘法写上去了····= = 好了 ...
随机推荐
- Linux C 程序 字符串运算符-表达式(TWO)
1.字符串常量 双引号"" :eg:"china" ,字符串在存储的时候会以一个\0为结束标志.2.符号常量 ,给常量取一个名字. #include< ...
- c++sort函数的用法浅析
(一)为什么要用c++标准库里的排序函数 Sort()函数是c++一种排序方法之一,学会了这种方法也打消我学习c++以来使用的冒泡排序和选择排序所带来的执行效率不高的问题!因为它使用的排序方法是类似于 ...
- Cent OS 常用 命令
1.开机自动联网操作 需要root权限 vim /etc/sysconfig/network-scripts/ifcfg-p4p1(p4p1 为网络链接名称)这个文件, 把ONBOOT="n ...
- Java学习-Overload和Override的区别
1.Overload是重载的意思,Override是覆盖的意思,也就是重写. 2.重载Overload表示同一个类中可以有多个名称相同的方法,但这些方法的参数列表各不相同(即参数个数或类型不同). 3 ...
- Oracle 分析函数之聚集函数(MAX、MIN、AVG和SUM)
MAX 查找组中表达式的最大值 MAX(COL ) OVER ( [ <partition_by_clause> ] < order_by_clause > )MIN 查找组中 ...
- Android openGL ES 2.0里Surfaceview背景透明
surfaceview的黑色背景会挡住其父的背景,现在把surfaceview的背景设为透明,既可以看到所绘的3D物体,又可以看到背景. 在onSurfaceCreated里,调用GLES20.glC ...
- new reader
BufferedReader bufferedReader = new BufferedReader( new StringReader(charset.decode(inBuffer).toStri ...
- C# winform 弹出输入框
Microsoft.VisualBasic.dll 引用using Microsoft.VisualBasic; string PM = Interaction.InputBox("提示 ...
- PHP之关闭网页错误提示
关闭PHP错误脚本提示是程序上线了必须做的一件事情,就是不管程序怎么报错我们都不能让错误日志在服务器上给大家看到,下面我来总结两种关闭PHP错误脚本提示的具体方法 最简单的办法就是直接在php程序代码 ...
- FZU 2150 Fire Game(BFS)
点我看题目 题意 :就是有两个熊孩子要把一个正方形上的草都给烧掉,他俩同时放火烧,烧第一块的时候是不花时间的,每一块着火的都可以在下一秒烧向上下左右四块#代表草地,.代表着不能烧的.问你最少花多少时间 ...