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 ...
随机推荐
- struts2笔记01-环境搭建
1.官网下载struts2 struts-2.3.28-all.zip,这个包可谓应有尽有,以后全靠它了! 2.jar包怎么选? (1)struts-2.3.28-all\struts-2 ...
- boa,thttp服务器安装,配置,测试
boa 1, SERVER_ROOT自定义,define.h头文件中,默认“/etc/boa" 2,./configure 3,修改CC,默认CC=gcc,make 4,error util ...
- J2SE知识点摘记(七)
1. 枚举的用法 enum 枚举名{枚举值表标};例子:"enum weekday{sun,mon,tue,wed,fri,sat}a,b,c;" For循环语句中使 ...
- 转:script中的async和defer
script中的async和defer defer: This Boolean attribute is set to indicate to a browser that the script is ...
- TLSAlloc()
为什么要有TLS?原因在于,进程中的全局变量与函数内定义的静态(static)变量,是各个线程都可以访问的共享变量.在一个线程修改的内存内容,对所有线程都生效.这是一个优点也是一个缺点.说它是优点,线 ...
- 使用session插件并且实现登录验证
var express = require('express'); var cookieParser = require('cookie-parser'); var bodyParser = requ ...
- kafka学习(四)-Topic & Partition
topic中partition存储分布 Topic在逻辑上可以被认为是一个queue.每条消费都必须指定它的topic,可以简单理解为必须指明把这条消息放进哪个queue里.为了使得 Kafka的吞吐 ...
- bootstrap栅格系统的div高度怎样定?
不能直接写px,不同设置分辨不同,div宽度始终是满屏的,如果高度指定像素大小,div就不能保证长宽比例恰当了. 解决方法: height:0px;padding-bottom:100%
- android 抽屉式滑动demo
下载地址:https://github.com/asijack/AndroidDrawerDemo 直接上效果图如下: 是不是还不错的样子. 先看看布局文件吧 <android.support. ...
- jquery prop()方法 解决全选 不全选 反选 问题 解决执行一次不不能再执行问题
//1.如果通过prop()函数更改<input>和<button>元素的type属性,在多数浏览器上将会抛出一个错误,因为该属性一般不允许在后期更改.//如果使用prop() ...