#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-高精度整数加法的更多相关文章

  1. POJ 1504 Adding Reversed Numbers (水题,高精度整数加法)

    题意:给两个整数,求这两个数的反向数的和的反向数,和的末尾若为0,反向后则舍去即可.即若1200,反向数为21.题目给出的数据的末尾不会出现0,但是他们的和的末尾可能会出现0. #include &l ...

  2. c++ 超长整数加法 高精度加法

    c++ 超长整数加法 高精度加法 实现思路 不能直接使用加法,因为int和long long都已超出最大数据表示范围 数据读入采用string类型,读入后将数据的每一位存储到vector中 vecto ...

  3. 高精度整数 - a+b(王道)

    题目描述: 实现一个加法器,使其能够输出a+b的值. 输入: 输入包括两个数a和b,其中a和b的位数不超过1000位. 输出: 可能有多组测试数据,对于每组数据,输出a+b的值 样例输入: 2 6 1 ...

  4. AC日记——大整数加法 openjudge 1.6 10

    10:大整数加法 总时间限制:  1000ms 内存限制:  65536kB 描述 求两个不超过200位的非负整数的和. 输入 有两行,每行是一个不超过200位的非负整数,可能有多余的前导0. 输出 ...

  5. JavaScript超大整数加法

    原文:JavaScript超大整数加法 什么是「超大整数」? JavaScript 采用 IEEE754标准 中的浮点数算法来表示数字 Number. 我也没花时间去详细了解 IEEE754标准 ,但 ...

  6. HDU1002——大整数加法

    题目: I have a very simple problem for you. Given two integers A and B, your job is to calculate the S ...

  7. 2981:大整数加法-poj

    2981:大整数加法 总时间限制:  1000ms 内存限制:  65536kB 描述 求两个不超过200位的非负整数的和. 输入 有两行,每行是一个不超过200位的非负整数,可能有多余的前导0. 输 ...

  8. RNN入门(4)利用LSTM实现整数加法运算

      本文将介绍LSTM模型在实现整数加法方面的应用.   我们以0-255之间的整数加法为例,生成的结果在0到510之间.为了能利用深度学习模型模拟整数的加法运算,我们需要将输入的两个加数和输出的结果 ...

  9. POJ 2506 Tiling(递推+大整数加法)

    http://poj.org/problem?id=2506 题意: 思路:递推.a[i]=a[i-1]+2*a[i-2]. 计算的时候是大整数加法.错了好久,忘记考虑1了...晕倒. #includ ...

随机推荐

  1. Windows下USB磁盘开发系列二:枚举系统中所有USB设备

    上篇 <Windows下USB磁盘开发系列一:枚举系统中U盘的盘符>介绍了很简单的获取系统U盘盘符的办法,现在介绍下如何枚举系统中所有USB设备(不光是U盘). 主要调用的API如下: 1 ...

  2. (Interface)接口特点

    接口是一种规范.只要一个类继承了一个接口,这个类就必须实现这个接口中所有的成员 为了多态. 接口不能被实例化.也就是说,接口不能new(不能创建对象) 接口中的成员不能加"访问修饰符&quo ...

  3. Linq to SQL 的增删改查操作

    Linq,全称Language Integrated Query,是C#语言的一个扩展,可以将数据查询直接集成到编程语言本身中. Linq分为查询语法和方法语法,说白了查询语法就是 from wher ...

  4. gitlab多人协作开发

    gitlab多人协同工作 本文为亨利向<Git权威指南>的作者蒋鑫老师的答疑邮件写成. 这里特别感谢蒋鑫老师对我询问gitlab的协同工作流程问题的详细解答. 蒋鑫老师的细致专业的解答让我 ...

  5. iOS·UIButton如何文字在下图片在上

    创建子类继承自UIButton,在layoutSubviews方法中改变文字和图片的位置就可以了,同理,稍作改变,可以写出文字在上图片在下.本文只给出文字在下图片在上的代码 -(void)layout ...

  6. 谈谈html5存储之IndexdDB

    IndexdDB简介 html5中indexdDB是一种能在浏览器持久的存储结构化数据的数据库:且具有丰富的查询能力. 新建一个IndexdDB数据库 IDBOpenDBRequest定义有几个重要的 ...

  7. PowerShell添加或修改注册表开机启动项脚本

    代码如下: $name = Read-Host "请输入开机启动项的名字(随便起)" $value = Read-Host "请输入开机启动项的值" try{ ...

  8. JAVA 多线程学习总结

    新手一枚,Java学习中,把自己学习多线程的知识总结一下,梳理下知识,方便日后查阅,高手莫进. 本文的主要内容: [1]    实现线程的两种方法                [2]  线程的启动与 ...

  9. python utf-8 配置

    环境:centos6.5,python 2.6 源码文档使用utf-8 #!/usr/bin/python # -*- coding: UTF-8 -*- 字符串默认用utf-8(不用在前面加u了) ...

  10. Mybatis配置文件

    XML 映射配置文件 MyBatis 的配置文件包含了设置(settings)和属性(properties)信息. properties 这些属性都是可外部配置且可动态替换的,既可以在典型的 Java ...