当前位置:网站首页>C语言 简单易懂的高精度加法
C语言 简单易懂的高精度加法
2022-07-06 07:12:00 【小妞无语】
场景
我想大家在小学刚学开始学习列式计算时,应该都有被老妈拎着耳朵说怎么又忘记进一了,都幻想有个机器可以帮自己计算,现在你算看到的高精度加法就是这个原理。
问题描述
它关键的点有简单的几个
1.数据存储问题
当我们的 int 和 longlong也没有办法满足这个数的存储大小时我们该怎么办?
首先想到的就是拆分啊,把一个很长很长的数字拆成一位又一位这样不就方便储存了,这就用到了数组存储的方法
2.拆分之后怎么进行运算
这就简单多了,就和我们小学学的一样满十进一原则
知道这些就ok了,下面看看代码的展示吧!
代码展示
#include<stdio.h>
#include<string.h>
char s1[505],s2[505];
int a[505],b[505],c[505];//定义里面外面都可以
int main()
{
int la,lb,lc;
scanf("%s",s1);
scanf("%s",s2);//由于不知道数字的长度,先将其定义为字符型·
la=strlen(s1);//直接计算长度没有‘/0’
lb=strlen(s2);
for(int i=0;i<la;i++)
a[la-i]=s1[i]-'0';//将字符数组变成数字型便于计算,并使其倒序存储
for(int i=0;i<lb;i++)
b[lb-i]=s2[i]-'0';
lc=(lb>la)?(lb+1):(la+1);//运用三目运算符,由于不论数有多大最大就是最长长度+1
for(int i=0;i<=lc;i++)
{
c[i]+=a[i]+b[i];//由于c[i]可能不为0,所以也要参与累加
c[i+1]=c[i]/10;//进位
c[i]=c[i]%10;//取余
}
if(c[lc]==0&&lc>0)//如果最高为0,就将其去除
lc--;
for(int i=lc;i>0;i--)
printf("%d",c[i]);
return 0;
}边栏推荐
- 1091: two or three things in childhood (multi instance test)
- Misc of BUU (update from time to time)
- leetcode1020. Number of enclaves (medium)
- How to find a medical software testing institution? First flight software evaluation is an expert
- librosa音频处理教程
- 中青看点阅读新闻
- 微信脑力比拼答题小程序_支持流量主带最新题库文件
- You deserve this high-value open-source third-party Netease cloud music player
- 【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
- “无聊猿” BAYC 的内忧与外患
猜你喜欢

ROS learning_ Basics

1091: two or three things in childhood (multi instance test)

Wechat official account infinite callback authorization system source code, launched in the whole network

Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Short video, more and more boring?

leetcode841. Keys and rooms (medium)

Internal and external troubles of "boring ape" bayc

The author is dead? AI is conquering mankind with art

Week6 weekly report
随机推荐
Configure raspberry pie access network
(4) Web security | penetration testing | network security web site source code and related analysis
leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
librosa音频处理教程
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
作者已死?AI正用藝術征服人類
The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module
Depth residual network
简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
CDN acceleration and cracking anti-theft chain function
Path analysis model
接口自动化测试框架:Pytest+Allure+Excel
A brief introduction of reverseme in misc in the world of attack and defense
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
Setting and using richview trvstyle template style
leetcode1020. Number of enclaves (medium)
C - Inheritance - polymorphism - virtual function member (lower)
因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
PCL realizes frame selection and clipping point cloud