【模板】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 ...
随机推荐
- mac 安装oh-my-zsh的问题
安装完,如果想切换回mac原来的bash终端,可以: chsh -s /bin/bash 反之,切换回zsh: chsh -s /bin/zsh
- 1656:Combination
一本通1656:Combination 1656:Combination 时间限制: 1000 ms 内存限制: 524288 KB提交数: 89 通过数: 49 [题目描述] ...
- 通过.zip安装eclipse插件
参考地址https://stackoverflow.com/questions/5482554/how-to-install-plugin-for-eclipse-from-zip
- ArcGIS超级工具SPTOOLS-拓扑错误处理
1.1 删除线面直线上的点 操作视频: https://weibo.com/tv/v/Hxjgmuv6F?fid=1034:4379388532225679 删除面要素.线要素一条边直线上的点. 1 ...
- H5本地存储详解
H5之前存储数据一般是通过 cookie ,但是 cookie 存的数据容量比较少.H5 中扩充了文件存储能力,可存储多达 5MB 的数据.现在就实际开发经验来对本地存储 ( Storage ) 的使 ...
- react native Expo适配全面屏/Expo识别全面屏和正常屏
一.最新版本的expo已经默认支持了全面屏,即不会像react native cli一样出现底部黑边 二.但是全面屏通过Dimensions.get('window')获取的高度还是不准确,因为全面屏 ...
- VBA 格式化excel数据表 (数据分列)
Sub ImportData() ' ' Copy Data from one workbook to the Current Workbook ' Place the macro file in t ...
- sqlmap注入工具----一次简单的注入(实战)
最近在学习网络安全,挖洞避免不了要使用许多工具,使用著名注入工具sqlmap的时候,对于英语不怎么好的我就比较难受了,本来就不会使用,加之又是英语的,简直难受.上网找了好多详细教程,但是命令实在是太多 ...
- sklearn常见分类器(二分类模板)
# -*- coding: utf-8 -*- import pandas as pd import matplotlib matplotlib.rcParams['font.sans-serif'] ...
- JAVA 基础编程练习题38 【程序 38 求字符串长度】
38 [程序 38 求字符串长度] 题目:写一个函数,求一个字符串的长度,在 main 函数中输入字符串,并输出其长度. package cskaoyan; public class cskaoyan ...