当前位置:网站首页>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;
}边栏推荐
- C语言 简单易懂的高精度加法
- 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
- chrome查看页面fps
- Project GFS data download
- 3. Business and load balancing of high architecture
- OpenJudge NOI 2.1 1661:Bomb Game
- [server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
- What is the biggest problem that fresh e-commerce is difficult to do now
- Thought map of data warehouse construction
- Short video, more and more boring?
猜你喜欢

WPF之MVVM

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

leetcode59. 螺旋矩阵 II(中等)

Depth residual network

Fast target recognition based on pytorch and fast RCNN

Top test sharing: if you want to change careers, you must consider these issues clearly!

树莓派3B更新vim

Kubernetes cluster builds ZABBIX monitoring platform

Wechat official account infinite callback authorization system source code, launched in the whole network
![[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5](/img/c3/7a147151b7338cf38ffbea24e8bafd.jpg)
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
随机推荐
BIO模型实现多人聊天
Three methods of adding color to latex text
leetcode6109. 知道秘密的人数(中等,周赛)
Project GFS data download
Top test sharing: if you want to change careers, you must consider these issues clearly!
Cookie Technology & session Technology & ServletContext object
Multi attribute object detection on rare aircraft data sets: experimental process using yolov5
Introduction to the basics of network security
First knowledge of OpenGL es learning (1)
Bloom taxonomy
UWA pipeline version 2.2.1 update instructions
数据仓库建设思维导图
Seriously recommend several machine learning official account
librosa音频处理教程
Week6 weekly report
A brief introduction of reverseme in misc in the world of attack and defense
编译,连接 -- 笔记 -2
PCL realizes frame selection and clipping point cloud
Configure raspberry pie access network
Win10 64 bit Mitsubishi PLC software appears oleaut32 DLL access denied