高精度加法--C++
高精度加法--C++
仿照竖式加法,在第一步计算的时候将进位保留,第一步计算完再处理进位。(见代码注释)
和乘法是类似的。
#include <iostream>
#include <cstring>
#define MAXSIZE 20
#define MAXOUTSIZE MAXSIZE + 2
using namespace std;
int main()
{
char a[MAXSIZE] = {'0'},
b[MAXSIZE] = {'0'},
c[MAXOUTSIZE] = {'\0'};
int a_int[MAXSIZE] = {0},
b_int[MAXSIZE] = {0},
c_int[MAXOUTSIZE] = {0};
cin >> a;
cin >> b;
int len_a = strlen(a), len_b = strlen(b);
memset(a_int, 0, sizeof a_int);
memset(b_int, 0, sizeof b_int);
memset(c_int, 0, sizeof c_int);
//对齐
for (int i = len_a - 1, j = 0; i >= 0; i--, j++)
{
a_int[j] = a[i] - '0';
}
for (int i = len_b - 1, j = 0; i >= 0; i--, j++)
{
b_int[j] = b[i] - '0';
}
//对应位相加,但是不处理进位
for (int i = 0; i < MAXSIZE; i++)
{
c_int[i] = a_int[i] + b_int[i];
}
//处理进位
int ans_len = 0;
for (int i = 0; i < MAXOUTSIZE - 1; i++)
{
c_int[i + 1] += c_int[i] / 10;
c_int[i] %= 10;
ans_len = i;
}
while (c_int[ans_len] == 0)
{
ans_len--;
}
if (ans_len < 0)
{
cout << "0";
return 0;
}
for (int i = ans_len; i >= 0; i--)
{
c[i] = c_int[i] + '0';
}
for (int i = MAXOUTSIZE - 1; i >= 0; i--)
{
if ('\0' != c[i])
{
cout << c[i];
}
}
return 0;
}
高精度加法--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 ...
- 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 ...
- SCAU1143 多少个Fibonacci数--大菲波数【杭电-HDOJ-1715】--高精度加法--Fibonacci数---大数比较
/*******对读者说(哈哈如果有人看的话23333)哈哈大杰是华农的19级软件工程新手,才疏学浅但是秉着校科联的那句“主动才会有故事”还是大胆的做了一下建一个卑微博客的尝试,想法自己之后学到东西都 ...
随机推荐
- MySQL自带的性能压力测试工具mysqlslap
mysqlslap是从MySQL的5.1.4版开始就开始官方提供的压力测试工具. 通过模拟多个并发客户端并发访问MySQL来执行压力测试,同时提供了较详细的SQL执行数据性能报告,并且能很好的对比多个 ...
- python虚拟环境 -- virtualenv , virtualenvwrapper
virtualenv -- python虚拟沙盒 有人说:virtualenv.fabric 和 pip 是 pythoneer 的三大神器. 一.安装 pip install virtualenv ...
- [Python_4] Python 面向对象(OOP)
0. 说明 Python 面向对象(OOP) 笔记.迭代磁盘文件.析构函数.内置方法.多重继承.异常处理 参考 Python面向对象 1. 面向对象 # -*-coding:utf-8-*- &quo ...
- Git & GitHub 的安装配置
参考 教你免费搭建个人博客,Hexo&Github 安装Git 1. 注册 GitHub 注册.登录 https://github.com/ 2. 创建仓库 在 GitHub 的右上角 ...
- MYSQL导入csv类型的数据出现The MySQL server is running with the --secure-file-priv option
今天尝试使用 into outfile导出数据的时候出现错误: The MySQL server is running with the --secure-file-priv option so it ...
- Dalvik指令备忘
跳转指令 if-eq vx, vy, 目标 如果vx == vy注2,跳转到目标.if-ne vx,vy, 目标 如果vx != vy注2,跳转到目标. if-lt vx,vy, 目标 如果vx &l ...
- php 错误1
Maximum execution time of 30 seconds exceeded 方法一,修改php.ini文件 max_execution_time = 30; Maximum execu ...
- leetcode 121 买卖股票的最佳时机
题目 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. ...
- [Eclipse]如何往eclipse中导入单个python文件,其它类型代码文件也可以参照该方法
实例:想从外部单独拷一个文件到项目中指定路径,如果直接拷到对应文件夹路径下,启动eslipse又识别不到该文件,下面介绍直接copy的方法至eclipse,复制成功后即会在项目中对应路径下产生文件,下 ...
- 1.Dubbo2.5.3源码编译
转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.安装JAVA.Git.Maven 安装过程省略,请自行百度. 2.编译dubbo (1)从ht ...