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) ...
随机推荐
- 使用prototype 对象定义类成员
使用prototype 对象定义类成员上一节介绍了类的实现机制以及构造函数的实现,现在介绍另一种为类添加成员的机制:prototype 对象.当new 一个function 时,该对象的成员将自动赋给 ...
- 1kkk
代码: # !usr/bin/python3.4 # -*- coding:utf-8 -*- import requests import os import time import re from ...
- Redis学习手册(List数据类型)
一.概述: 在Redis中,List类型是按照插入顺序排序的字符串链表.和数据结构中的普通链表一样,我们可以在其头部(left)和尾部(right)添加新的元素.在插入时,如果该键并不存在,Redis ...
- selenium+python自动化之环境安装
一.Python安装 1.操作系统:win7 64位系统 2.下载Python安装包,选择2.7版本和3.6版本都可以(最好安装2.7版本稳定)官网下载地址:https://www.python.or ...
- 安卓模拟器安装apk,上网
1.首先找到安装安卓模拟器的文件夹: 2.目录:D:\Java\Android-all\platform-tools 3.运行cmd命令:adb install D:\Java\Android-all ...
- C语言qsort函数用法
qsort函数简介 排序方法有很多种:选择排序,冒泡排序,归并排序,快速排序等. 看名字都知道快速排序是目前公认的一种比较好的排序算法.因为他速度很快,所以系统也在库里实现这个算法,便于我们的使用. ...
- linux shell 整理收集(不断更新)
1)主从复制延时判断 (转 http://www.cnblogs.com/gomysql/p/3862018.html) 说明: 不要通过Seconds_Behind_Master去判断,该值表示sl ...
- MyBatis SQL xml处理小于号与大于号
MyBatis SQL xml处理小于号与大于号 当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台 ...
- DW(五):polybase集群安装
目录: Prerequisites 集群配置规划 polybase install firewall config 集群配置 删除计算节点 install Prerequisites Microsof ...
- 配置Windows Server2008+iis+php+mysql所需下载安装包
最近一个朋友让我帮忙给配置服务器iis+php+mysq 环境,遇到了很多问题,特此就在这里说一下.小弟只是在windwos2003 和windwos XP下配置过iis+php+mysql,去朋友那 ...