当前位置:网站首页>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;
}
边栏推荐
- 这个高颜值的开源第三方网易云音乐播放器你值得拥有
- The best way to learn SEO: search engine
- ORACLE列转行--某字段按指定分隔符转多行
- Project GFS data download
- 19. Actual memory management of segment page combination
- Solution to the problem of breakthrough in OWASP juice shop shooting range
- 数据仓库建设思维导图
- Interface automation test framework: pytest+allure+excel
- Bloom taxonomy
- LeetCode Algorithm 2181. Merge nodes between zero
猜你喜欢
Go learning -- implementing generics based on reflection and empty interfaces
Internal and external troubles of "boring ape" bayc
OpenGL ES 学习初识(1)
JDBC learning notes
leetcode1020. 飞地的数量(中等)
Thought map of data warehouse construction
呆错图床系统源码图片CDN加速与破解防盗链功能
18. Multi level page table and fast table
Go learning --- use reflection to judge whether the value is valid
The author is dead? AI is conquering mankind with art
随机推荐
SEO学习的最好方式:搜索引擎
hydra常用命令
LeetCode Algorithm 2181. 合并零之间的节点
微信公众号无限回调授权系统源码 全网首发
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
多线程和并发编程(二)
Thought map of data warehouse construction
Establishment and operation of cloud platform open source project environment
Refer to how customer push e-commerce does content operation
18. Multi level page table and fast table
Path analysis model
LeetCode Algorithm 2181. Merge nodes between zero
Simple use of JWT
Misc of BUU (update from time to time)
A brief introduction of reverseme in misc in the world of attack and defense
这个高颜值的开源第三方网易云音乐播放器你值得拥有
How are the open source Netease cloud music API projects implemented?
What is the biggest problem that fresh e-commerce is difficult to do now
The difference between get and post request types
树莓派3B更新vim