nyoj 103 A + B problem II
A+B Problem II
- 描述
-
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
A,B must be positive.
- 输入
- 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. - 输出
- 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. - 样例输入
-
2
1 2
112233445566778899 998877665544332211 - 样例输出
-
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
大数加法,以前写的,可读性很差,唯一有用的地方是reverse函数,用来逆置一个字符串
#include<string.h>
#include<stdio.h>
#include<iostream>
#include <algorithm>
using namespace std;
char str[2][1002];
int main(){
int len[2];
int num;
bool flag;
int i , j;
scanf("%d", &num);
for(j= 1; j <=num; j++) {
memset(str, 0, sizeof(str));
scanf("%s %s", str[0], str[1]);
printf("Case %d:\n", j);
printf("%s + %s = " , str[0] , str[1]);
len[0] = strlen(str[0]);
len[1] = strlen(str[1]);
reverse(str[0], str[0] + len[0]);
reverse(str[1], str[1] + len[1]);
if(len[0] > len[1]) flag = 0;
else flag = 1;
for(i= 0 ; str[!flag][i]; i++){
str[flag][i] += str[!flag][i] - '0';
}
for(i = 0 ;str[flag][i]; i++){
if(str[flag][i] > '9'){
if(str[flag][i + 1] < '0')
str[flag][i + 1] = '1';
else
str[flag][i + 1] ++;
str[flag][i] -= 10;
}
}
len[flag] = strlen(str[flag]);
reverse(str[flag], str[flag] + len[flag]);
printf("%s\n" , str[flag]);
}
return 0;
}
nyoj 103 A + B problem II的更多相关文章
- nyoj 623 A*B Problem II(矩阵)
A*B Problem II 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 ACM的C++同学有好多作业要做,最头痛莫过于线性代数了,因为每次做到矩阵相乘的时候,大 ...
- nyoj 103-A+B Problem II (python 大数相加)
103-A+B Problem II 内存限制:64MB 时间限制:3000ms 特判: No 通过数:10 提交数:45 难度:3 题目描述: I have a very simple proble ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- HDU 1002 A + B Problem II
A + B Problem II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted ...
- A + B Problem II
之前总是在查阅别人的文档,看着其他人的博客,自己心里总有一份冲动,想记录一下自己学习的经历.学习算法有一段时间了,于是想从算法开始自己的博客生涯O(∩_∩)O~~ 今天在网上看了一道大数相加(高精度) ...
- hdu 1023 Train Problem II
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...
- HDU1002 -A + B Problem II(大数a+b)
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过
杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...
- hdoj 1002 A + B Problem II
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- windows系统安装MongoDB
最近一直在学习node.js,nodejs开发指南中有一个微博的web开发项目,由于该书出的比较早(2012出的),目前为止利用nodejs进行web开发各种组合技术都发生了很大的更新,例如书中选择的 ...
- selenium+python自动化之元素定位
自动化按步骤拆分的话,可以分为四步操作:定位元素,操作元素,获取返回结果,断言(返回结果与期望结果是否一致),最后自动出测试报告.本篇接下来讲基本的八种元素定位方法.说的通俗一点,就是教大家找对象. ...
- Centos Mysql 升级
如何升级CentOS 6.5下的MySQL CentOS 6.5自带安装了MySQL 5.1,但5.1有诸多限制,而实际开发中,我们也已经使用MySQL 5.6,这导致部分脚本在MySQL 5.1中执 ...
- oracle分析函数 之分组累加求和
select s.slice_date_to ,s.made_id ,sum(s.steup_count)over(partition by s.made_id order by s.slice_da ...
- Tomcat连接池
步骤1: 找到Tomcat安装目录下的context.xml文件,在config目录下.在<Context/>节点下加入: <Resource name="jdbc/myt ...
- ASIHttpRequest addRequestHeader的处理
我现在用到了ASIHttpRequest来读取数据,这时候我发现里面的头在很多时候都是一样的.如用户的令牌,设备类型,ios版本,软件版本等 [self.request addRequestHeade ...
- Android开发——通过扫描二维码,打开或者下载Android应用
Android开发——通过扫描二维码,打开或者下载Android应用 在实现这个功能的时候,被不同的浏览器折磨的胃疼,最后实现了勉强能用,也查考了一下其他人的博客 android实现通过浏览器点击 ...
- 51nod 1056
n<=10000000000 然后欧拉函数的前缀和可以用莫比乌斯函数的前缀和快速求,注意各种取模 #include<cstdio> typedef long long i64; ,P ...
- 86. Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- PHPWord生成word实现table合并(colspan和rowspan)
PHPWord(http://phpword.codeplex.com/)是一个很好处理和生成WORD文档的工具,但是生成复杂的word,如colspan和rowspan的实现,还是需要你做些修改. ...