当前位置:网站首页>Simple and understandable high-precision addition in C language
Simple and understandable high-precision addition in C language
2022-07-06 07:14:00 【The chick is speechless】
scene
I think when you first started to learn determinant calculation in primary school , I should have been held by my mother's ears to say how I forgot to enter one again , They all fantasize about having a machine that can help them calculate , Now you can see that this is the principle of high-precision addition .
Problem description
Its key points are simple
1. Data storage problems
When our int and longlong There is no way to meet this number of storage size, what should we do ?
The first thought is to split , It's not convenient to store a long number by breaking it into one digit after another , And that's where it comes in Array storage Methods
2. How to calculate after splitting
It's much simpler , Just like what we learned in primary school The principle of full decimal
Just know this ok 了 , Let's take a look at the code display !
Code display
#include<stdio.h>
#include<string.h>
char s1[505],s2[505];
int a[505],b[505],c[505];// Define both inside and outside
int main()
{
int la,lb,lc;
scanf("%s",s1);
scanf("%s",s2);// Because I don't know the length of the number , First define it as character type ·
la=strlen(s1);// Directly calculate the length without ‘/0’
lb=strlen(s2);
for(int i=0;i<la;i++)
a[la-i]=s1[i]-'0';// Turn the character array into numeric type for easy calculation , And store them in reverse order
for(int i=0;i<lb;i++)
b[lb-i]=s2[i]-'0';
lc=(lb>la)?(lb+1):(la+1);// Use the binocular operator , Because no matter how big the number is, the maximum is the longest length +1
for(int i=0;i<=lc;i++)
{
c[i]+=a[i]+b[i];// because c[i] Probably not. 0, So we should also participate in accumulation
c[i+1]=c[i]/10;// carry
c[i]=c[i]%10;// Remainder
}
if(c[lc]==0&&lc>0)// If the maximum is 0, Remove it
lc--;
for(int i=lc;i>0;i--)
printf("%d",c[i]);
return 0;
}
边栏推荐
- Babbitt | metauniverse daily must read: the group image of Chinese Internet enterprises pouring into metauniverse: "there are only various survival desires, and there is no ambition for forward-lookin
- 树莓派3B更新vim
- 作者已死?AI正用藝術征服人類
- TS Basics
- leetcode841. 钥匙和房间(中等)
- Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
- A brief introduction of reverseme in misc in the world of attack and defense
- Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
- leetcode704. Binary search (find an element, simple, different writing)
- Three methods of adding color to latex text
猜你喜欢
WPF之MVVM
Twelve rules for naming variables
JDBC学习笔记
Path analysis model
SSM学习
leetcode6109. 知道秘密的人数(中等,周赛)
Win10 64 bit Mitsubishi PLC software appears oleaut32 DLL access denied
Cookie Technology & session Technology & ServletContext object
Solution to the problem of breakthrough in OWASP juice shop shooting range
C - Inheritance - polymorphism - virtual function member (lower)
随机推荐
Multi attribute object detection on rare aircraft data sets: experimental process using yolov5
升级版手机检测微信工具小程序源码-支持多种流量主模式
PCL realizes frame selection and clipping point cloud
Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
UDP攻击是什么意思?UDP攻击防范措施
Arduino tutorial - Simon games
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
这个高颜值的开源第三方网易云音乐播放器你值得拥有
SSM learning
RichView TRVStyle 模板样式的设置与使用
杰理之蓝牙设备想要发送数据给手机,需要手机先打开 notify 通道【篇】
Seriously recommend several machine learning official account
[hot100] 739. Température quotidienne
Missing monitoring: ZABBIX monitors the status of Eureka instance
Uni app third party package configuration network request
What is the biggest problem that fresh e-commerce is difficult to do now
hydra常用命令
CDN acceleration and cracking anti-theft chain function
(4) Web security | penetration testing | network security web site source code and related analysis
漏了监控:Zabbix对Eureka instance状态监控