A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 261413    Accepted Submission(s): 50581

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
 
Author
Ignatius.L
 

思路:

       将数字以字符的形式存储到字符数组中,由于在存储的时候是高位在以0为下标的下标变量中存储的。所以要将其进行翻转,存储到整形数组中(也就是高位存储到大下标变量中。由于在进位的时候能在原来的基础上进行i++。来存储最高位的数据)。然后将两个大数按位相加。假设比十大,进行进位操作!
 

代码:

#include <stdio.h>
#include <string.h>
#define N 10005
char a[N],b[N];
int c[N],d[N];
int main()
{
int n,i,j,k,len1,len2;
scanf("%d",&n);
k=n;
while(n--)
{
memset(c,0,sizeof(c));//每次都得清零,所以得放到while循环里面。
memset(d,0,sizeof(d));
getchar();
scanf("%s%s",a,b);//空格也是scanf的切割符!
len1=strlen(a);
len2=strlen(b);
for(i=len1-1,j=0;i>=0;i--)//由于须要逆序保存,所以应该设变量j从0開始! c[j++]=a[i]-'0';
for(i=len2-1,j=0;i>=0;i--)
d[j++]=b[i]-'0';
for(i=0;i<1001;i++)
{
c[i]+=d[i];
if(c[i]>=10)
{
c[i]-=10;
c[i+1]++;
}
}
printf("Case %d:\n%s + %s = ",k-n,a,b);
for(i=1000;i>=0&&c[i]==0;i--);
if(i>=0)
for(;i>=0;i--)
{
printf("%d",c[i]);
}
else
printf("0");
printf("\n");
if(n!=0)
printf("\n");
}
return 0;
}

HPU 1002 A + B Problem II【大数】的更多相关文章

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

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

  2. hdu1002 A + B Problem II(大数题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java/ ...

  3. Problem : 1002 ( A + B Problem II )

    经验总结:一定要注意输出的格式,字符的空格,空行,一定要观察清楚.如本题的最后一个输出结果后面没有空行.最后代码实现的时候需要判断一下,代码如下 !=n) cout<<endl; Prob ...

  4. HDU1002 A + B Problem II 大数问题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java ...

  5. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

  6. hdoj 1002 A + B Problem II【大数加法】

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

  7. 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) ...

  8. hduoj 1002 A + B Problem II

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目描述如下: A + B Problem II Time Limit: 2000/1000 M ...

  9. HDU 1002 A + B Problem II

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

随机推荐

  1. 使用UDEV SCSI规则在Oracle Linux上配置ASM

    对于使用ASM管理的磁盘来说,需要一种能够用于一致性标识磁盘设备及其正确的所属关系和权限的手段.在Linux系统中,可以使用ASMLib来执行这项任务,但是这样做的缺点是在操作系统上增加了额外的一层, ...

  2. [转]linux之磁盘配额(quota)

    转自:http://www.jb51.net/LINUXjishu/78446.html 磁盘配额(quota)比较常用的几个情况是: * 针对WWW server,例如:每个人的网页空间的容量限制 ...

  3. [转]Android自定义Adapter的ListView的思路及代码

    本文转自:http://www.jb51.net/article/37236.htm 在开发中,我们经常使用到ListView这个控件.Android的API也提供了许多创建ListView适配器的快 ...

  4. 【反射】Java反射机制

    Class 1.Class是一个类,一个描述类的类(也就是描述类本身),封装了描述方法的Method,描述字段的Filed,描述构造器的Constructor等属性    2.对象照镜子后(反射)可以 ...

  5. 使用Dreamweaver在一张图片上添加多个热点链接

    所有代码: <html> <head> <meta charset="utf-8"> <title>无标题文档</title& ...

  6. TensorFlow学习---入门(一)-----MNIST机器学习

    参考教程:http://www.tensorfly.cn/tfdoc/tutorials/mnist_beginners.html 数据下载地址:http://wiki.jikexueyuan.com ...

  7. 4th 循环结构概述和for语句的格式及其使用

    04.01_Java语言基础(循环结构概述和for语句的格式及其使用) A:循环结构的分类 for,while,do...while B:循环结构for语句的格式: for(初始化表达式;条件表达式; ...

  8. Hibernate自动事务揪出的编码不规范

    最近重构的项目(Java初学中),Service层一个获取通知记录报错: org.springframework.dao.InvalidDataAccessResourceUsageException ...

  9. MSSQL高并发下生成连续不重复的订单号

    一.确定需求 只要做过开发的基本上都有做过订单,只要做过订单的基本上都要涉及生成订单号,可能项目订单号生成规则都不一样,但是大多数规则都是连续增长. 所以假如给你一个这样的需求,在高并发下,以天为单位 ...

  10. 微信小程序支付(JSAPI支付)

    开发环境:.NET MVC+ ORM框架(EF) 一.参考文档: 1.微信JSAPI支付官方文档:https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api. ...