高精度加法--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级软件工程新手,才疏学浅但是秉着校科联的那句“主动才会有故事”还是大胆的做了一下建一个卑微博客的尝试,想法自己之后学到东西都 ...
随机推荐
- Node.js环境搭建和学习(windwos环境)
Node.js环境搭建和学习 一.环境搭建 1.下载安装文件 下载地址http://nodejs-org.qiniudn.com/下载Node.js环境安装包,根据操作系统下载对应的安装包 下载地址 ...
- linux 设备驱动加载的先后顺序
Linux驱动先注册总线,总线上可以先挂device,也可以先挂driver,那么究竟怎么控制先后的顺序呢. 1.初始化宏 Linux系统使用两种方式去加载系统中的模块:动态和静态. 静态加载:将所有 ...
- Oracle 导出用户下的所有索引创建语句
SELECT dbms_lob.substr(dbms_metadata.get_ddl('INDEX', INDEX_NAME))||';' from dba_indexes where owne ...
- 微信小程序点击 navigator ,页面不跳转
1.navigator 对应的 url 必须配置在app.json的pages中: 2.navigator 对应的 url 不能配置在"tabBar"的"list&quo ...
- linux下的“定时器”:crontab
1.概述 crontab是用来设置在固定时间点或时间间隔执行某条指令,类似于时程表.使用-u user是指定user用户的时程表. 2.参数 -e[UserName] :调出编辑器,编辑定时任务,打开 ...
- Linux版的Mimikaz
A tool to dump the login password from the current linux desktop user. Adapted from the idea behind ...
- 或许因为缺少默认route配置而导致的的ping超慢,甚至timeout
内网有台dell r620 做测试用: 4个网口都插了 网线,通过小交换直接连在中心路由的第3个口上: 安装了kvm,有virbr0 :但还没装其它的虚拟机. ping 外网域名 ,IP地址 都超 ...
- /etc/hosts,GoldenGate
[oracle@g]$ netstat -alp|grep 7809(Not all processes could be identified, non-owned process info wil ...
- [转]OpenGL通过VBO实现顶点数组绘制顶点
#include "stdlib.h" #include <OpenGL/glext.h> #include <GLUT/GLUT.h> #define B ...
- Python:基础知识
python是一种解释型.面向对象的.带有动态语义的高级程序语言. 一.下载安装 官网下载地址:https://www.python.org/downloads 下载后执行安装文件,按照默认安装顺序安 ...