无线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 ...
随机推荐
- vs打开项目出现“尚未配置为Web项目XXXX指定的本地IIS URL HTTP://localhost:…… .要打开此项目,需要配置虚拟目录……”提示
今天打开网上下载的一个源码,出现如标题的这个问题,这是从未遇见的提示.尝试点击是,但是网站还是运行不起来.于是网上搜索,就有了这篇. 解决的方案如下: 注意:也可以用记事本把工程文件(.vcxproj ...
- [To do]Appx Package installed, can't start at first time
When installed partition tool & mkrcvcd as windows store app. it can't start at first time. the ...
- php时间戳之间相互转换
第一种情况: 将时间戳转换成年月日格式 <?php echo date('Y-m-d H:i:s',$v['apply_time']);?> 第二种情况: 将年月日转换成时间戳 strto ...
- ftp协议详解
客户端与服务器之间,需要多条连接才能完成应用的协议,属于复杂协议.如FTP,PPTP,H.323和SIP均属于复杂协议. 这里主要介绍ftp协议的工作原理.首先,ftp通信协议有两种工作模式,被动模式 ...
- 模块(configparser+shutil+logging)
一.configparser模块 1.模块介绍 configparser用于处理特定格式的文件,其本质上是利用open来操作文件. *注:(这里解释一下特定格式的文件) a.有section和opti ...
- webapi 通过dynamic 接收可变参数
public class JsonParamModel { /// <summary> /// json key /// </summary> public string Js ...
- Android 通过JNI实现守护进程,使得Service服务不被杀死
来自: http://finalshares.com/read-7306 转载请注明出处: http://blog.csdn.net/yyh352091626/article/details/5054 ...
- webdriver杀死浏览器和Chromedriver进程
/** * 执行dos命令 * @param command */ public static void command(String command) { ...
- 长时间停留在calculating requirements and dependencies 解决方案
如果Eclipse花费了很长的时间calculating requirements and dependencies(计算需求和依赖性 ) 这个问题通常就是在点击安装之后显示“Calculating ...
- 读《编写可维护的JavaScript》第九、十章总结
第九章 将配置数据从代码中分离出来 9.2 抽离配置数据 这章比较好理解,也非常常见,作者给的俩个例子就能说明一切: // 将配置数据藏在代码中 function validate(value) { ...