无线OSS-高精度整数加法
#include<iostream>
#include<string>
using namespace std; int compareStr(string str1, string str2)
{
int num1 = str1.length();
int num2 = str2.length();
if(num1>num2) return 1;
if(num1<num2) return -1;
if(num1==num2)
{
for(int i=0; i<num1; i++ )
{
if(str1[i]>str2[i])
{
return 1;
}
else if(str1[i]<str2[i])
{
return -1;
}
else
{ }
}
return 0;
}
} string rever(string str)
{
int num = str.length();
string tmp = str;
for(int i=0; i<num; i++)
{
tmp[i] = str[num-1-i];
}
return tmp;
} string mns(string str1, string str2)
{
int num1 = str1.length();
int num2 = str2.length(); string tmp1 = rever(str1);
string tmp2 = rever(str2); char buf[100];
int count = 0; int carry = 0;
for(int i=0; i<num1; i++)
{
if(i<num2)
{
if(tmp1[i]-'0'-carry >= tmp2[i]-'0')
{
char ch = tmp1[i]-'0'-carry - (tmp2[i]-'0') + '0';
buf[count++] = ch;
carry = 0;
}
else
{
char ch = tmp1[i]-'0'-carry - (tmp2[i]-'0') + 10 + '0';
buf[count++] = ch;
carry = 1;
}
}
else
{
if(tmp1[i]-'0'-carry >= 0)
{
char ch = tmp1[i]-'0'-carry;
buf[count++] = ch;
carry = 0;
}
else
{
char ch = '9';
buf[count++] = ch;
carry = 1;
}
}
} buf[count] = '\0'; for(count--; count>0 && buf[count]=='0' ; count--);
buf[count+1] = '\0'; return rever(buf);
} string add(string str1, string str2)
{
int num1 = str1.length();
int num2 = str2.length(); int maxnum = num1 > num2 ? num1 : num2;
int minnum = num1 > num2 ? num2 : num1; string tmp1 = rever(str1);
string tmp2 = rever(str2); char buf[100];
int count = 0; int carry = 0;
for(int i=0; i<maxnum; i++)
{
if(i<minnum)
{
if((tmp1[i]+tmp2[i]-'0'-'0' + carry)>=10)
{
char ch = (tmp1[i]+tmp2[i]-'0'-'0'-10) + '0' + carry; //res = res + string(&ch);
buf[count++] = ch;
carry = 1;
}
else
{
char ch = (tmp1[i]+tmp2[i]-'0'-'0') + '0' + carry;
//res = res + string(&ch);
buf[count++] = ch;
carry = 0;
}
}
else
{
if(num1 > num2)
{
if( (carry + tmp1[i] - '0') >= 10 )
{
char ch = (tmp1[i]-'0'-10) + carry + '0';
//res = res + string(&ch);
buf[count++] = ch;
carry = 1;
}
else
{
char ch = (tmp1[i]-'0') + carry + '0';
//res = res + string(&ch);
buf[count++] = ch;
carry = 0;
}
}
else
{
if( (carry + tmp2[i] - '0') >= 10 )
{
char ch = (tmp2[i]-'0'-10) + carry + '0';
//res = res + string(&ch);
buf[count++] = ch;
carry = 1;
}
else
{
char ch = (tmp2[i]-'0') + carry + '0';
//res = res + string(&ch);
buf[count++] = ch;
carry = 0;
}
}
}
} if(carry==1)
{
char ch = '1';
//res = res + string(&ch);
buf[count++] = ch;
} buf[count] = '\0'; return rever(buf);;
} void solve(string str1, string str2)
{
if(str1[0]=='-' && str2[0]=='-')
{
string tmp1;
string tmp2;
tmp1 = str1.substr(1);
tmp2 = str2.substr(1);
cout<<'-'<<add(tmp1,tmp2);
}
else if(str1[0]=='-' && str2[0]!='-')
{
string tmp1;
string tmp2;
tmp1 = str1.substr(1);
tmp2 = str2;
if(compareStr(tmp1,tmp2)>0)
{
cout<<'-'<<mns(tmp1,tmp2);
}
else if(compareStr(tmp1,tmp2)==0)
{
cout<<'0';
}
else
{
cout<<mns(tmp2,tmp1);
}
}
else if(str1[0]!='-' && str2[0]=='-')
{
string tmp1;
string tmp2;
tmp1 = str1;
tmp2 = str2.substr(1);
if(compareStr(tmp1,tmp2)>0)
{
cout<<mns(tmp1,tmp2);
}
else if(compareStr(tmp1,tmp2)==0)
{
cout<<'0';
}
else
{
cout<<'-'<<mns(tmp2,tmp1);
}
}
else
{
string tmp1 = str1;
string tmp2 = str2;
cout<<add(tmp1,tmp2);
}
} int main()
{
string str1, str2;
cin>>str1>>str2; solve(str1, str2); return 0;
}
无线OSS-高精度整数加法的更多相关文章
- POJ 1504 Adding Reversed Numbers (水题,高精度整数加法)
题意:给两个整数,求这两个数的反向数的和的反向数,和的末尾若为0,反向后则舍去即可.即若1200,反向数为21.题目给出的数据的末尾不会出现0,但是他们的和的末尾可能会出现0. #include &l ...
- c++ 超长整数加法 高精度加法
c++ 超长整数加法 高精度加法 实现思路 不能直接使用加法,因为int和long long都已超出最大数据表示范围 数据读入采用string类型,读入后将数据的每一位存储到vector中 vecto ...
- 高精度整数 - a+b(王道)
题目描述: 实现一个加法器,使其能够输出a+b的值. 输入: 输入包括两个数a和b,其中a和b的位数不超过1000位. 输出: 可能有多组测试数据,对于每组数据,输出a+b的值 样例输入: 2 6 1 ...
- AC日记——大整数加法 openjudge 1.6 10
10:大整数加法 总时间限制: 1000ms 内存限制: 65536kB 描述 求两个不超过200位的非负整数的和. 输入 有两行,每行是一个不超过200位的非负整数,可能有多余的前导0. 输出 ...
- JavaScript超大整数加法
原文:JavaScript超大整数加法 什么是「超大整数」? JavaScript 采用 IEEE754标准 中的浮点数算法来表示数字 Number. 我也没花时间去详细了解 IEEE754标准 ,但 ...
- HDU1002——大整数加法
题目: I have a very simple problem for you. Given two integers A and B, your job is to calculate the S ...
- 2981:大整数加法-poj
2981:大整数加法 总时间限制: 1000ms 内存限制: 65536kB 描述 求两个不超过200位的非负整数的和. 输入 有两行,每行是一个不超过200位的非负整数,可能有多余的前导0. 输 ...
- RNN入门(4)利用LSTM实现整数加法运算
本文将介绍LSTM模型在实现整数加法方面的应用. 我们以0-255之间的整数加法为例,生成的结果在0到510之间.为了能利用深度学习模型模拟整数的加法运算,我们需要将输入的两个加数和输出的结果 ...
- POJ 2506 Tiling(递推+大整数加法)
http://poj.org/problem?id=2506 题意: 思路:递推.a[i]=a[i-1]+2*a[i-2]. 计算的时候是大整数加法.错了好久,忘记考虑1了...晕倒. #includ ...
随机推荐
- SQL2008代理作业出现错误: c001f011维护计划创建失败的解决方法
SQL2008数据库总会出现从 IClassFactory 为 CLSID 为 {17BCA6E8-A95D-497E-B2F9-AF6AA475916F} 的 COM 组件创建实例失败,原因是出现以 ...
- 正则提取 html 里<input> 标记的value 值
获取html 标记的值: :年月日 结果:您选择的是2014年1月22日 使用了Regex 对象,得到一个 MatchCollection,然后进行处理. string mes = @"&l ...
- Js 拖动效果
<!DOCTYPE html> <html> <head> <meta charset="utf8"> <title>j ...
- CryptoJS DES加密
<!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equ ...
- ffmpeg centos6.5上安装(测试 amr 转换为 mp3)
1. 下载相关源码包 wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz wget http://sourcefo ...
- (转)深入浅出 妙用Javascript中apply、call、bind
原文连接 深入浅出 妙用Javascript中apply.call.bind 网上文章虽多,大多复制粘贴,且晦涩难懂,我希望能够通过这篇文章,能够清晰的提升对apply.call.bind的认识,并且 ...
- IntelliJ IDEA常用快捷键windows
1 Alt+回车 导入包,自动修正 Ctrl+N 查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L 格式化代码 Ctrl+Alt+O 优化导入的类和包Alt+Insert 生成代码( ...
- 简单介绍一下R中的几种统计分布及常用模型
统计学上分布有很多,在R中基本都有描述.因能力有限,我们就挑选几个常用的.比较重要的简单介绍一下每种分布的定义,公式,以及在R中的展示. 统计分布每一种分布有四个函数:d――density(密度函数) ...
- 怎样处理“error C2220: warning treated as error - no object file generated”错误
最近用VS2010 编译ceflib开源库是出现"怎样处理"error C2220: warning treated as error - no object file gener ...
- FPGA重要设计思想
FPGA重要设计思想 1.速度和面积互换原则.以面积换速度可以实现很高的数据吞吐率,其实串/并转换.就是一种以面积换速度的思想 2.乒乓操作. 3.串/并转换的思想. 高速数据处理的重要技巧之一. ...