HDU1002 Problem II(大数相加)(C++题解)
A题 HDOJ1002(大数加法)
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 483720 Accepted Submission(s): 93351
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 <iostream> #include <cstdio> #include <cstring> using namespace std; int main(){char a[8000],b[8000];int an[8000],bn[8000],sum[8000],pre,flag=1;int t;scanf("%d",&t);while(t--){memset(an,0,sizeof(an));memset(bn,0,sizeof(bn));memset(sum,0,sizeof(sum));scanf("%s%s",a,b);int len1=strlen(a);int len2=strlen(b);int len=(len1>len2)?len1:len2;pre=0;for(int i=0;i<len1;i++){an[len1-i-1]=a[i]-'0';}for(int j=0;j<len2;j++){bn[len2-j-1]=b[j]-'0';}for(int k=0;k<len;k++){sum[k]=an[k]+bn[k]+pre/10;pre=sum[k];}if(pre>9){sum[len]=pre/10%10;len++;pre/=10;}printf("Case %d:\n",flag++);printf("%s + %s = ",a,b);for(int s=0;s<len;s++){printf("%d",sum[len-s-1]%10);}printf("\n");if(t){printf("\n");}}return 0; }
转载于:https://www.cnblogs.com/jianqiao123/p/11195380.html
HDU1002 Problem II(大数相加)(C++题解)相关推荐
- hdu A + B Problem II(大数相加,数组实现)
hdu A + B Problem II(大数相加,数组实现) 题目走起 注意最后一个case 不需要换行 下面代码 #include<stdio.h> #include<strin ...
- HDU - 1002 A + B Problem II(大数)
题目链接:点击查看 题目大意:大数加法 题目分析:第一次用Java写ACM,还是有点小激动滴,有几点需要注意的: 公共类名要写Main 不要提交包名 注意输入输出 上代码吧,纪念一下: import ...
- A + B Problem II 大数加法
题目描述: Input The first line of the input contains an integer T(1<=T<=20) which means the number ...
- hdu1002 a+bII 大数相加
表示无力吐槽!!又错了好几次 发表以明志!!! I have a very simple problem for you. Given two integers A and B, your job i ...
- HPU 1002 A + B Problem II【大数】
HPU 1002 A + B Problem II[大数] A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- java棋盘问题_0x03大数问题(JAVA解决棋盘覆盖,A+B Problem II)
常见的有棋盘覆盖和A+B问题,这类问题牵扯到的数值都比较大,如果用一般的数值类型,肯定输出不了,所以就要想一个办法,怎么把大数转换一下输出. A+B Problem II 时间限制:3000 ms | ...
- HDU.1002 A + B Problem II
原题 HDU.1002 A + B Problem II 分类 杂题·大位整数运算 题意 计算从任意两个长度在1000以内的正整数的和. 输入/输出 要求与格式 输入内容 最开始一行开始输入样例数,对 ...
- 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 ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
最新文章
- 日读论文100+,AI都替代不了!辞去医药研发总监后,她成为了一名全职学术警察...
- SWFLibrary类加载外部资源库
- Lucene教程--入门程序详解
- Access 和 SQL 数据类型的比较
- linux 正则查找email_Linux 正则表达式详解
- mysql数据库入门教程(2):常见命令大全,语法规范
- 1007. 素数对猜想
- C++中用frugally-deep调用keras的模型并进行预测
- 台式计算机技术方案,2017年4月自考02316计算机应用技术真题及答案
- HDU 3018 Ant Trip(一笔画问题)
- centos安装python gcc sqlite
- (译)如何使用cocos2d制作一个塔防游戏:第三部分
- Apache Wicket 7.2.0发布
- 适合用于UI设计器/开发人员探索您或他人创建的UI的一款UI设计开发软件——QuickLens for Mac
- java 分布式 定时任务_Java中实现分布式定时任务的方法
- 爆笑校园生活——人在校园的日子
- Linux man帮助文档
- Flask教程(十九)SocketIO
- 构建器builder模式 + lombok @Builder的介绍及使用
- Zookeeper重要概念
热门文章
- Ext.widgets-Button,SplitButton,CycleButton
- ubuntu 网卡双网口 配置_Ubuntu 18.04 设置多网卡多端口聚合
- python中实例方法与实例属性-Python中的类属性和实例属性以及静态方法和类方法...
- python装饰器类-python_类装饰器
- python基本语法语句-Python基本语句
- 中国最大的python社区-python求最大数
- python3哪个教程好-Python3 教程
- 学好python可以做什么兼职-自学Python能干些什么副业
- python是c语言写的吗-python是用c写的吗
- python项目开发实战-给缺少Python项目实战经验的人