无线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 ...
随机推荐
- Material Design Lite,简洁惊艳的前端工具箱 之 容器组件。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博客地址为http://www.cnblogs.com/jasonnode/ .网站上有对应每一 ...
- bootstraptable插件文档的坑
1.事件onCheck中文档参数为row, $element正确的顺序为$element,row eg: $Table.on('check.bs.table', function ($element, ...
- MS CRM 2013 Plugin 注册工具登录后空白
解决办法 把en-us, zh-CN 目录随便改个名字就好了
- 日常总结——JSP篇(补)
序—— 初次接触JSP,写一个登录注册界面 正文—— JSP介绍:JSP通过在标准的HTML页面中插入java代码,其静态的部分无须java程序控制.每个JSP页面就是一个servlet实例, WEB ...
- Centos7下用命令同步标准时间
新装的CentO7S系统服务器可能设置了错误的时间,需要调整时区和时间.如下是CentOS7系统使用NTP协议,从一个时间服务器同步标准时间: [root@localhost ~]# cp /usr/ ...
- python_函数
一.map 遍历序列,对序列中每个元素进行操作,最终获取新的序列 li = [11,22,33,44] new_list = map(lambda a: a + 100,li) print(new_l ...
- java.sql.SQLException: ORA-00972: 标识符过长
经仔细检查,发现sql语句其中两个字段之间没有逗号!
- cocos2dx UI总结
1.a->addChild(b); 如果b是一个layer,则默认是忽略锚点的,此时无论你怎么设置它的锚点都没用,必须先b->ignoreAnchorPointForPosition(fa ...
- 在现有的图像处理软件中融合dxf格式输出
在现有的图像处理软件中融合dxf格式输出 dxf格式是autocade的支持格式.如果将现有图像识别的结果导出到dxf格式,就能够使用autocad的强大功能进行后续处理. dxf的格式比较复杂,开源 ...
- 学生信息管理系统(cocos2d引擎)——数据结构课程设计
老师手把手教了两天半,看了一下模式,加了几个功能就大功告成了!!! 给我的感想就是全都是指针! 添加图片精灵: CCSprite* spBG = CCSprite::create("&qu ...