无线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 ...
随机推荐
- web应用安全防御100技 好书再次阅读, 变的只是表象,被概念迷惑的时候还是静下心来回顾本质
如何进行web应用安全防御,是每个web安全从业者都会被问到的问题,非常不好回答,容易过于肤浅或流于理论,要阐明清楚,答案就是一本书的长度.而本文要介绍一本能很好回答这个问题的优秀书籍——<we ...
- android使用ksoap2调用sap的webservice
public void on_clicked(View view) { Thread webserviceThread = new Thread() { public void run() { Str ...
- Asp.net mvc 添加Jquery UI
1.使用NuGet下载jquery ui Install-Package jQuery.UI.Combined 2.下载多语言文件: development-bundle/i18n目录下是jquery ...
- C# 隐藏标题栏 调整大小 并且移动窗口
隐藏标题栏(窗口属性): 1.设置在该窗体的标题栏中是否显示控件框: this.ControlBox = false; 2.设置在该窗体的标题为空: this.Text = string.Empty; ...
- 使用java mail 发送邮件
1.关联jar包: activation.jar mail.jar 2.调用 @Test public void test1() { List<String> imageUrlLi ...
- JavaScript简介
JavaScript JavaScript 是一种轻量级的编程语言,是可插入 HTML 页面的编程代码,这门语言可用于 HTML 和 web,更可广泛用于服务器.PC.笔记本电脑.平板电脑和智能手机等 ...
- java是值传递还是引用传递
首先写一个简便的Employee,以便测试使用. class Employee { private String name; public Employee(String name) { this.n ...
- [课程设计]Sprint Three 回顾与总结&发表评论&团队贡献分
Sprint Three 回顾与总结&发表评论&团队贡献分 ● 一.回顾与总结 (1)回顾 燃尽图: Sprint计划-流程图: milestones完成情况如下: (2)总结 本次冲 ...
- [课程设计]Scrum 3.8 多鱼点餐系统开发进度(留言反馈系统设计)
Scrum 3.8 多鱼点餐系统开发进度(留言反馈系统设计) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到店点餐系统 ...
- java性能监控常用命令
jps -m -l:主要用来输出JVM中运行的进程状态信息 jstack -l pid 来观察锁持有情况 jsatck pid | grep pid(十六进制):输出进程pid的堆栈信息 jmap - ...