当前位置:网站首页>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;
}边栏推荐
- Markdown 中设置图片图注
- TS Basics
- JDBC学习笔记
- 18. Multi level page table and fast table
- How to find a medical software testing institution? First flight software evaluation is an expert
- 将ue4程序嵌入qt界面显示
- UWA pipeline version 2.2.1 update instructions
- 网络安全基础介绍
- UDP攻击是什么意思?UDP攻击防范措施
- Three methods of adding color to latex text
猜你喜欢

Blue Bridge Cup zero Foundation National Championship - day 20

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

SSM学习

Raspberry pie serial port login and SSH login methods

【每日一题】729. 我的日程安排表 I

【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例

NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections

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

首发织梦百度推送插件全自动收录优化seo收录模块

Internal and external troubles of "boring ape" bayc
随机推荐
可变参数重载时的内存错误
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
UDP攻击是什么意思?UDP攻击防范措施
CDN acceleration and cracking anti-theft chain function
网络安全基础介绍
UWA Pipeline 2.2.1 版本更新说明
Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
3. Business and load balancing of high architecture
“无聊猿” BAYC 的内忧与外患
chrome查看页面fps
SSM学习
SEO学习的最好方式:搜索引擎
Compile, connect -- notes-2
Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file
Depth residual network
The difference between get and post request types
What is the biggest problem that fresh e-commerce is difficult to do now
树莓派串口登录与SSH登录方法
这个高颜值的开源第三方网易云音乐播放器你值得拥有
Crawling exercise: Notice of crawling Henan Agricultural University