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. ACM_梦中的函数

    梦中的函数 Time Limit: 2000/1000ms (Java/Others) Problem Description: 寒假那段时间,每天刷题的小G连做梦都是代码,于是有了这道题. 给定一个 ...

  2. Elasticsearch之CURL命令的bulk批量操作

    大家,也可去看看我下面的博客 Elasticsearch之批量操作bulk 官网上,是举例了新建一个requests文件. [hadoop@master elasticsearch-]$ pwd /h ...

  3. Ambari架构及安装

    不多说,直接上干货! 1.什么是Ambari? 2.Ambari项目是由哪几部分构成的? 3.Ambari系统架构是如何组成的? 前言 Hadoop集群的管控一直是一个热门的话题,对于这样的一个应用场 ...

  4. cmd 启动mysql环境变量配置

    win10系统:(其他系统类似,改环境变量就可以) 1.我的电脑,右键选择属性,进入系统页面 2.点击高级系统设置,进入系统属性页面 3.点击高级选项卡,点击环境变量,进入环境变量设置 4.选择系统变 ...

  5. 我的 Windows 10 的基本配置

    Windows 10 的基本配置 功能性 开启 .Net Framework 3.5(包括 .NET 2.0 和 3.0) 旧版本 Windows 10 默认只安装了 .Net Framework 4 ...

  6. Python之操作Excel、异常处理、网络编程

    知识补充: 1.falsk模块中一些方法总结 import flask from flask import request,jsonify server = flask.Flask(__name__) ...

  7. mysql5.7初始化密码报错ERROR1820(HY000):YoumustresetyourpasswordusingALTERUSERstateme

    1,mysql5.6是密码为空直接进入数据库的,但是mysql5.7就需要初始密码 cat /var/log/mysqld.log | grep password 或者:grep 'temporary ...

  8. java中一个数组不能放不同数据类型的值

    在java中,数组不能放不同数据类型的值. 方法一: 多态 定义数组类型的时候定义为父类,而存进数组为父类的子类 public class test2 { public static void mai ...

  9. mybatis批量操作(foreach)

    foreach可以在SQL语句中通过拼接的方式进行集合迭代.foreach元素的属性主要有collection,item,index,separator,open,close. item属性:表示循环 ...

  10. Light Oj - 1134 Be Efficient

    题目传送门:Be Efficient 题意:输入n和m,然后输入有n个元素的一个序列,问有多少个子序列元素的和能整除m. 思路:求前缀和,利用一个前缀的一个定理求解. 前缀和的一个定理是:每次求的前缀 ...