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 ...
随机推荐
- poj 3378 Crazy Thairs dp+线段树+大数
题目链接 题目大意: 给出n个数, 让你求出有多少个5元组满足 i < j < k < l < m并且ai < aj < ak < al < am 我们 ...
- ELMAH+MVC4+SQLite 错误日志
任何程序我想无论是在调试开发阶段还是上线运营阶段,都能够使人“放心”,不要出什么意外,也不要玩什么心跳:那就需要比较到位和及时的异常与错误日志模块. 本文将简要描述ELMAH.MVC4与SQLite这 ...
- 查看ORACLE中正在运行的存储过程 kill
1:登陆PLSQL Developer,写一个存储过程,向一个表中插入值,并运行存储过程 2:打开PLSQL Developer的命令窗口 .--终止procedure 11.select * f ...
- EditText无法失去焦点、失去焦点隐藏软键盘
很奇怪,我在给EditText设置setOnFocusChangeListener()监听,但是始终未能成功,焦点一直存在,不知其原因,,代码如下: et_username.setOnFocusCha ...
- 紫薇~还记得大明湖畔的HTML5智力拼图吗?
曲线谜团是非常有趣的HTML5智力游戏,据说超过多少分会有惊喜,游戏简单易操作,偶尔抛弃那种杀死脑细胞的大型游戏,玩玩这种简单经典的益智小游戏,放松放松,也是不错的选择嘛-将游戏 通过 统一开发环境( ...
- iOS6和iOS7代码的适配(1)
苹果的iOS7推出后,对于所有的应用来说都提出了一个天然的需求--适配不同版本的SDK.目前来说,用iOS6的SDK生成的应用,可以在iOS7的系统上跑,UI上也保持了原来的风格样式,这是苹果做的向下 ...
- cors技术
简称跨域资源共享: 若是配置nodejs: 需在公共路由添加三句话:代码如下: // 全局头设置 app.all('*', function(req, res, next) { res.set({ ' ...
- android插件化-apkplugdemo源代码阅读指南-10
阅读本节内容前可先了解 apkplug基础教程 本教程是基于apkplug V1.6.8 版本号编写 最新开发方式以官网为准 可下载最新的apkplugdemo源代码http://git.oschi ...
- Asp.net 获取服务器指定文件夹目录文件,并提供下载
string dirPath = HttpContext.Current.Server.MapPath("uploads/"); if (Directory.Exists(dirP ...
- android入门——Activity(2)
主要内容:一.IntentFlag 二.简单复杂数据传递 三.数据回传 四.打开系统界面 五.IntentFilter匹配 一.IntentFlag 复制一段内容 来源 http://i ...