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

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

 

用字符数组保存大数,模拟手算注意进位处理,以及第一位数是否进位。

 
 

#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的更多相关文章

  1. 51nod 1005 大数加法

    #include<iostream> #include<string> using namespace std; #define MAXN 10001 },b[MAXN]={} ...

  2. c#大数加法

    在C#中,我们经常需要表示整数.但是,c#的基本数据类型中,最大的long也只能表示-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807之间的数 ...

  3. 玲珑杯1007-A 八进制大数加法(实现逻辑陷阱与题目套路)

    题目连接:http://www.ifrog.cc/acm/problem/1056 DESCRIPTION Two octal number integers a, b are given, and ...

  4. Leetcode 67 Add Binary 大数加法+字符串处理

    题意:两个二进制数相加,大数加法的变形 大数加法流程: 1.倒置两个大数,这一步能使所有大数对齐 2.逐位相加,同时进位 3.倒置两个大数的和作为输出 class Solution { public: ...

  5. HDU1002大数加法

    大数加法 c++版: #include <map> #include <set> #include <stack> #include <queue> # ...

  6. java实现大数加法、乘法(BigDecimal)

    之前写过用vector.string实现大数加法,现在用java的BigDecimal类,代码简单很多.但是在online-judge上,java的代码运行时间和内存大得多. java大数加法:求a+ ...

  7. vector、string实现大数加法乘法

    理解 vector 是一个容器,是一个数据集,里边装了很多个元素.与数组最大的不同是 vector 可以动态增长. 用 vector 实现大数运算的关键是,以 string 的方式读入一个大数,然后将 ...

  8. 【大数加法】POJ-1503、NYOJ-103

    1503:Integer Inquiry 总时间限制:  1000ms 内存限制:  65536kB 描述 One of the first users of BIT's new supercompu ...

  9. A + B Problem II 大数加法

    题目描述: Input The first line of the input contains an integer T(1<=T<=20) which means the number ...

  10. NI笔试——大数加法

    NI笔试: 1.找出字符串第一次出现的字符.用数组建立哈希表,然后再扫描字符串并判断次数是否为1. 2.大数加法,即字符串加法.因为之前写过乘法,就以为是乘法.然后就把乘法写上去了····= = 好了 ...

随机推荐

  1. 关于css中overflow:hidden的使用

    overflow:hidden有两个用处经常用到: 1.通过设定自身的高度,加上overflow:hidden可以隐藏超过容器本身的内容:     但是,小编在以往的使用中,发现了一个问题,只要父级容 ...

  2. yii2配置表前缀

    前缀设置 component中db的配置修改 'db'=>array( 'connectionString' => 'mysql:host=localhost;dbname=xxxx', ...

  3. #Leet Code# Permutation

    描述: 输出全排列 代码: class Solution: # @param num, a list of integer # @return a list of lists of integers ...

  4. Nodejs初学者福音

    Nodejs+Express+MongoDb 搭建个人博客  001 我喜欢把任务或者工作分解成工作流来完成,如下,后面将会按照流程来详述,希望能为Nodejs初学者及推广Nodejs做出些努力. n ...

  5. 解决lucene 重复索引的问题

    在使用Lucene过程中,会发现当我们为添加新的Document时,会产生重复现象(两次添加同一个Document),毕竟Lucene中没有像数据库中一样,有键可以区分.不过我们可以通过为Docume ...

  6. 安装cocoaPods遇到的坑

    第一个坑 更换ruby源后,安装cocoaPods是出现以下错误:activesupport requires Ruby version >= 2.2.2 Ruby version >= ...

  7. 代码审查工具 StyleCop 的探索

    最近我们Advent Data Service (ADS) 在项目上需要按照代码规范进行代码的编写工作,以方便将来代码的阅读与维护. 但是人工检查起来容易遗漏或者格式不统一, ReSharper又是收 ...

  8. [转载]C# 多选功能(checkedListBox控件)

    // 全选; private void btn_allSelected_Click(object sender, EventArgs e) { //this.CheckedListBox1.Check ...

  9. Ubuntu下与菜单和图标相关的几个文件夹

    转自UBUNTU下与菜单和图标相关的几个文件夹 /usr/share/icons  系统图标文件夹 /usr/share/applications  系统菜单文件夹,要在左上角的应用程序菜单中添加一项 ...

  10. http://www.ruanyifeng.com/blog/2011/09/restful

    http://www.ruanyifeng.com/blog/2011/09/restful