【模板】C++高精度加法
if(x.length() < y.length()) //保证y始终时是位数较短的数字字符串
swap(x, y);
int delta = x.length() - y.length(); //计算两个字符串的长度差
for(int i = ; i < delta; i++) //在较短的字符串前补0,使其长度等于较长的字符串的长度
y = "" + y;
之后开始从低位到高位进行运算
#include <iostream>
#include <string>
#include <algorihtm>
using namespace std;
string add_int(string_x, string _y)
{
string x = _x, y = _y;
string result;
int jw = ; if(x.length() < y.length()) //保证y始终时是位数较短的数字字符串
swap(x, y);
int delta = x.length() - y.length(); //计算两个字符串的长度差
for(int i = ; i < delta; i++) //在较短的字符串前补0,使其长度等于较长的字符串的长度
y = "" + y;
//从低位到高位进行运算
for(int i = x.length() - ; i >= ; i--)
{
int a, b ,sum;
a = x[i] - '';
b = y[i] - '';
sum = a + b + jw;
if(sum >= )
{
jw = ;
result += char(sum % + '');
}
else
{
jw = ;
result += char(sum % + '');
}
}
//若循环结束后,仍有进位,说明结果超出了加数中较长的一个的位数,根据加法的法则,应在结果前面加“1”
if(jw == )
result += "";
//反转结果。因为前面是从后往前运算,而字符串是从前往后加的,所以要反转一下
reverse(result.begin(), result.end());
return result;
}
int main()
{
string a, b;
cin >> a >> b;
cout << add_int(a, b);
return ;
}
声明:CSDN上的账号“奔跑的小蜗牛”也是我本人的账号
【模板】C++高精度加法的更多相关文章
- NEFU 2016省赛演练一 F题 (高精度加法)
Function1 Problem:F Time Limit:1000ms Memory Limit:65535K Description You know that huicpc0838 has b ...
- java算法 蓝桥杯 高精度加法
问题描述 在C/C++语言中,整型所能表示的范围一般为-231到231(大约21亿),即使long long型,一般也只能表示到-263到263.要想计算更加规模的数,就要用软件来扩展了,比如用数组或 ...
- 用c++实现高精度加法
c++实习高精度加法 最近遇到一个c++实现高精度加法的问题,高精度问题往往十复杂但发现其中的规律后发现并没有那么复杂,这里我实现了一个整数的高精度加法,主要需要注意以下几点: 1:将所需输入的数据以 ...
- 高精度加法——经典题 洛谷p1601
题目背景 无 题目描述 高精度加法,x相当于a+b problem,[b][color=red]不用考虑负数[/color][/b] 输入输出格式 输入格式: 分两行输入a,b<=10^500 ...
- 高精度加法--C++
高精度加法--C++ 仿照竖式加法,在第一步计算的时候将进位保留,第一步计算完再处理进位.(见代码注释) 和乘法是类似的. #include <iostream> #include < ...
- hdu1002 A + B Problem II(高精度加法) 2016-05-19 12:00 106人阅读 评论(0) 收藏
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- POJ 3181 Dollar Dayz(全然背包+简单高精度加法)
POJ 3181 Dollar Dayz(全然背包+简单高精度加法) id=3181">http://poj.org/problem?id=3181 题意: 给你K种硬币,每种硬币各自 ...
- leetcode 67. Add Binary (高精度加法)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- leetcode 66. Plus One(高精度加法)
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
随机推荐
- prometheus简单监控Linux,mysql,nginx
prometheus安装 下载安装 #官网下载 解压即可使用 https://prometheus.io/download/ #docker 方式安装 sudo docker run -n prome ...
- Hdu 5344
Hdu5344 题意: 给你一个数组,求所有的 $ a_i + a_j $ 的异或值. 解法: 因为 $ (a_i+a_j) \bigoplus (a_j + a_i) = 0$ . 所以答案就是 $ ...
- 为ubuntu找个能用的桌面,顺便进行适当的改造
最近,开始玩ros,结果官方推荐ubuntu linux,虽然都是差不多的linux,无非就是软件包形式不同而已.但是,个人对ubuntu的unity桌面实在是没有爱,最让人无语的是,kubuntu无 ...
- How do negative margins in CSS work and why is (margin-top:-5 != margin-bottom:5)?
How do negative margins in CSS work and why is (margin-top:-5 != margin-bottom:5)? 解答 Negative mar ...
- JS模拟Touch事件
var ele = document.getElementsByClassName('target_node_class')[0] //may have x and y properties in s ...
- CentOS7重启后resolv.conf被重置的解决方案
近期在修改一台CentOS7服务器的dns时发现只要重启服务器DNS就会被强制还原,解决方案如下: 1.首先在网卡设置中修改NM_CONTROLLED的值: 修改文件/etc/sysconfig/ne ...
- C之输入输出
%d - int%ld – long (long int)%lld - long long%hd – short 短整型 (half int) %c - char%f - float%lf – dou ...
- (翻译) How variables are allocated memory in Javascript? | scope chain | lexicial scope
总结: 阅读下面文章需要15分钟 提问者的问题是JavaScript中内存是怎么分配的,在介绍的过程作者涉及计到了JS中 Scope Chain和调用函数call生成lexicial environm ...
- ubuntu 安装的一些软件
https://github.com/wszqkzqk/deepin-wine-ubuntu 14 版本的ubuntu 安装发生问题; 16 18 的都可以安装成功微信;
- python 内置数据结构 字符串
字符串 一个个字符组成的有序的序列,是字符的集合 使用单引号,双引号,三引号引住的字符序列 字符串是不可变对象 Python3起,字符串就是Unicode类型 字符串定义 初始化 s1 = 'stri ...