HPU 1002 A + B Problem II【大数】

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; }

posted on 2017-07-03 10:41 mthoutai 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/mthoutai/p/7109952.html

HPU 1002 A + B Problem II【大数】相关推荐

  1. HDU - 1002 A + B Problem II(大数)

    题目链接:点击查看 题目大意:大数加法 题目分析:第一次用Java写ACM,还是有点小激动滴,有几点需要注意的: 公共类名要写Main 不要提交包名 注意输入输出 上代码吧,纪念一下: import ...

  2. HDU.1002 A + B Problem II

    原题 HDU.1002 A + B Problem II 分类 杂题·大位整数运算 题意 计算从任意两个长度在1000以内的正整数的和. 输入/输出 要求与格式 输入内容 最开始一行开始输入样例数,对 ...

  3. hdu A + B Problem II(大数相加,数组实现)

    hdu A + B Problem II(大数相加,数组实现) 题目走起 注意最后一个case 不需要换行 下面代码 #include<stdio.h> #include<strin ...

  4. HDOJ 1002 A + B Problem II 解题报告

    今天研究 1002,,,搞得半死,虽说是一道简单的题目,真正做起来绝不是很容易的... A + B Problem II Time Limit: 2000/1000 MS (Java/Others)  ...

  5. Hdu - 1002 - A + B Problem II

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

  6. HDU1002 Problem II(大数相加)(C++题解)

    A题 HDOJ1002(大数加法) A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/327 ...

  7. HDU 1002 A + B Problem II

    ACM OJ题解目录 本题网址:https://cn.vjudge.net/problem/HDU-1002 A + B Problem II I have a very simple problem ...

  8. hdu 1002 A+B problem II

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目分析:高精度问题,大整数相加. 由此题可见java相较于C/C++的优势,即丰富多彩的类以及 ...

  9. A + B Problem II 大数加法

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

最新文章

  1. 《从零开始学Swift》学习笔记(Day 47)——final关键字
  2. cx_oracle写日志信息_日志系统的设计
  3. 机器学习读书笔记(一)
  4. 一文揭秘定时任务调度框架quartz
  5. MySQL学习(四、子查询)
  6. bzoj1967 [AHOI2005]穿越磁场 离散最短路
  7. Java 将一段时间以周、月、季分割
  8. Android 功能系列篇
  9. 波特率和比特率的关系
  10. 闪迪u盘量产工具U盘正常显示但是多出几个空分区的解决方案
  11. 【jdbc】 jndi与“Java:comp/env/“语法
  12. 数据分析与AI(七)傅里叶对登月图片降噪/scipy库对图片进行处理/
  13. R语言之基本统计分析
  14. 使用JSP/Servlet技术开发新闻发布系统
  15. 【源码】紫色UI趣味测试小程序源码各种测试(趣味测试、爱情测试、缘分测试、性格测试)
  16. Comet OJ 夏季欢乐赛 Gree的心房
  17. English-人事部翻译资格认证
  18. 【数据库视频】报表服务
  19. JQuery语法知识
  20. C++ 模板类的继承

热门文章

  1. UpdateProgress控件
  2. 漫步微积分三十一——定积分的直观含义
  3. DCP(Deep Closest Point)论文阅读笔记以及详析
  4. LeetCode —— 365. 水壶问题(Python3)
  5. python发动机悬置解耦计算-按重心处整车坐标系解耦
  6. TensorFlow第七步再试牛刀-自编BP代码解Mnist
  7. ss模型复模态的物理意义及adams复模态振型求解
  8. Matlab--三种归一化方法
  9. 理科生的人生感悟-01-时间的短暂 - 父亲 - 火车上的颠沛流离
  10. 专业修改数据库服务器,专业修改数据库服务器