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) ...
随机推荐
- MySQL数据库update更新子查询
比如: ? 1 2 3 4 UPDATE test.tb_vobile a set a.name = '111 ' WHERE a.id = (select max(id) id from test. ...
- ASP.NET MVC3升级到ASP.NET MVC4 的方法
ASP.NET MVC3升级 ASP.NET MVC4 的方法: 1.先去掉引用的System.Web.Mvc.dll(MVC3版本),重新引入System.Web.Mvc.dll(MVC4版本) 2 ...
- 【转】JDBC为什么要使用PreparedStatement而不是Statement
http://www.importnew.com/5006.html PreparedStatement是用来执行SQL查询语句的API之一,Java提供了 Statement.PreparedSta ...
- 【Java编程进阶-1】enum枚举的使用
枚举主要用于枚举常量,下面举个简单的应用. 比如一个公司有如下几个部门: 研发部: 销售部: 财务部: (其他部门暂时不列举) 部门的某些信息相对固定,此时可以考虑使用枚举来说明: 枚举类 Depts ...
- bzoj1760 [Baltic2009]Triangulation
给定一个多边形的三角剖分(n<=1e5),且每个三角形有其颜色,问最多可以把这个三角剖分分成几个联通的部分,使任何一种颜色不出现在多个连通块中 建出三角剖分对应的树,同种颜色的点之间的路径是不能 ...
- form 转json,将form表单中的数据序列化数组后转换为Json
页面中引用了jquery,第一想到的就是序列化,但是序列化后的表单字段为a=1&b=2这种. 这里写一个jquery的扩展方法 $.fn.serializeObject = function( ...
- java获取url中的参数
获取地址栏中的url中的userName的值 String userName=new String(request.getParameter("userName")); 获取中文的 ...
- showdialog窗体不在任务栏显示的问题处理
场景: c#开发的windows窗体用showdialog弹出时,在任务栏中 win7系统显示,win xp和win 2003却不显示. 窗体的ShowInTaskbar已设置为True. 解决: 在 ...
- 【转】SQL SERVER 开窗函数简介
在SQL SERVER 2005/2008支持两种排名开窗函数和聚集开窗函数. 以SQL SERVER中分面页为例,按时间顺序列出定单号. WITH OrderInfo AS ( SELECT ROW ...
- hdu 5698 瞬间移动(排列组合)
这题刚看完,想了想,没思路,就题解了 = = 但不得不说,找到这个题解真的很强大,链接:http://blog.csdn.net/qwb492859377/article/details/514781 ...