HDU 1002 A + B Problem II(大整数相加)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Description
Input
not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
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<string>
#include<cstdio>
#include<vector> using namespace std; int t;
string str1,str2;
vector <char> v; void solve(){
string temp;
int a,l2;
if(str1.length()<str2.length()){
temp=str1;str1=str2;str2=temp;
l2=str2.length();
}
for(int i=0;i<str1.length()-l2;i++){
str2.insert(0,1,'0');
}
for(int i=0;i<str1.length();i++){
a=str1[str1.length()-i-1]+str2[str2.length()-i-1]-2*'0';
if(a>=10){
v.push_back(a%10+'0');
if(str1.length()-i-1==0){
v.push_back('1');break;
}
str1[str1.length()-i-2]=(char)(str1[str1.length()-i-2]+1);
}
else v.push_back((char)(a+'0'));
}
vector<char>::iterator it=v.end();
it--;
while(it!=v.begin()){
if(*it=='0')
v.erase(it);
else break;
it--;
}
for(int i=v.size()-1;i>=0;i--){
cout<<v[i];
}
cout<<endl;
} int main(){
int casen=0;
scanf("%d",&t);
while(t-->0){
cin>>str1>>str2;
printf("Case %d:\n%s + %s = ",++casen,str1.c_str(),str2.c_str());
v.clear();
solve();
if(t!=0)
cout<<endl;
}
return 0;
}
HDU 1002 A + B Problem II(大整数相加)的更多相关文章
- 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)
数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...
- hdu 1002.A + B Problem II 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目意思:就是大整数加法. 两年几前做的,纯粹是整理下来的. #include <stdi ...
- HDU 1002 A + B Problem II(高精度加法(C++/Java))
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1002 A + B Problem II
A + B Problem II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted ...
- hdu 1002 A + B Problem II(大正整数相加)
代码: #include<cstdio> #include<cstring> #define Min(a,b) ((a)<(b)?(a):(b)) using names ...
- HDU 1002 - A + B Problem II - [高精度]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 Problem DescriptionI have a very simple problem ...
- 大数加法~HDU 1002 A + B Problem II
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 题意: 数学题,A+B; 思路,这个数非常大,普通加法一定会超时,所以用大数加法.大数加法的基 ...
- HDU 1002 A + B Problem II(AC代码)
#include <stdio.h> #include <string.h> #define MAX 1009 int main() { },b[MAX]={}; ,z=,r= ...
- HDU 1002 A + B Problem II (大数加法)
题目链接 Problem Description I have a very simple problem for you. Given two integers A and B, your job ...
随机推荐
- 函数指针 如:void (*oper)(ChainBinTreee *p)
在C语言中,一个函数总是占用一段连续的内存区,而函数名就是该函数所占内存区的首地址.我们可以把函数的这个首地址(或称入口地址)赋予一个指针变量,使该指针变量指向该函数.然后通过指针变量就可以找到并调用 ...
- 基本数据结构简介--ath9k网卡驱动开发总结(二)
ath9k驱动代码主要数据结构概览. (1)在ath9k的驱动中,几乎是最顶层的数据结构是ath_softc,这个数据结构几乎随处可见.ath_softc是硬件与MAC层进行交互的中间载体,很多有用的 ...
- AutoIt 函数学习之----Send函数
Send: 作用:向激活窗口发送模拟键击操作. 语法: send('按键'[,标志]) 参数: 按键:要发送的按键序列. 标志:[可选参数] 更改程序处理“按键”的方式: 标志 = 0 (默认),按 ...
- 20.org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actua ...
- windows环境中mysql忘记root密码的解决办法
原文地址:http://www.cnblogs.com/linuxnotes/archive/2013/03/09/2951101.html windows下重置Mysql Root密码的方法mysq ...
- [C#]Stream.Write Extension Method
在处理Stream型态时常会使用到Stream.Write这个方法,每次都会有种疑问就是,大多数的处理都是要将Buffer整个写入,為何偏偏每次都要将索引带0,长度带為Buffer的大小呢?另外在处理 ...
- CSV 客座文章系列: Pruffi 通过 Windows Azure 挖掘社交媒体的强大招聘潜能
编辑人员注释:今天这篇文章由 Pruffi 创始人 Alena Vladimirskaya 和 Pruffi 的 CTO Alexander Ivanov 联合撰写,介绍了该公司如何使用 Window ...
- OpenGL绘制简单场景,实现旋转缩放平移和灯光效果
本项目实现了用OpenGL绘制一个简单场景,包括正方体.球体和网格,实现了物体的旋转.缩放.平移和灯光效果.附有项目完整代码.有具体凝视.适合刚開始学习的人熟悉opengl使用. 开发情况 开发环境V ...
- html5 绘制集合图形
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- angular的那些事
angular.js是什么 AngularJS 是一个 JavaScript 框架.它可通过 <script> 标签添加到 HTML 页面. AngularJS 通过 指令 扩展了 HTM ...