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) ...
随机推荐
- 最长公共字串(LCS)最长连续公共字串(LCCS)
链接1:http://blog.csdn.net/x_xiaoge/article/details/7376220 链接2:http://blog.csdn.net/x_xiaoge/article/ ...
- js跳转到页面中指定的hash
location.hash = "#filter_moreClue";
- DBA常用SQL之数据库基础信息
第一部分: 1. 查看oracle最大连接数 sql>show parameter processes #最大连接数 2. 修改最大连接数 sql>alter system set pro ...
- cmd命令运行php,php通过cmd运行文件
一.cmd命令运行php 1.通过配置环境变量 >php "php文件" 如果要待参数 php -q "php文件" 参数 php获取参数 $a = $a ...
- 【LVM】LVM自动扩容脚本
概要说明: /dev/mapper/vg0-data挂载在目录/data下: 当新增物理磁盘时,需要自动对/dev/mapper/vg0-data磁盘时进行扩容(自动化脚本): 当新增物理磁盘时,自动 ...
- 【转】关于 hashCode() 你需要了解的 3 件事
在 Java 中,每一个对象都有一个容易理解但是仍然有时候被遗忘或者被误用的 hashCode 方法.这里有3件事情要时刻牢记以避免常见的陷阱. 一个对象的哈希码允许算法和数据结构将对象放入隔间,就象 ...
- Python Paramiko模块
代码来自:http://www.cnblogs.com/wupeiqi/articles/4356675.html 今天学到了一个运维方面的利器:python的paramiko模块. paramiko ...
- rpm包安装时发现缺少其他依赖
多年来一直困扰我的问题,就是当我们下载了一个rpm包来安装的时候发现缺少依赖.以前的做法是网上挨个去搜索依赖的rpm,然后依次安装. # rpm -ivh google-chrome-stable_c ...
- FastReport使用总结三——条码简介
FastReport Desinger中支持的Barcode类型如下图所示: 设置其Barcode属性可以实现支持不同的条码类型. 注意: 支持的条码类型说明如下: 总结: 1.通过设置Barcode ...
- 连接数据库的URL等于多少?
JDBC编程步骤如下: 1.加载驱动 Class.forname(driverClass); 比如:加载MySQL的驱动 Class.forname("com.mysql.jdbc.Driv ...