当前位置:网站首页>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;
}边栏推荐
猜你喜欢

Go learning --- use reflection to judge whether the value is valid

MPLS experiment

L'auteur est mort? Ai utilise l'art pour conquérir l'humanité

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

The author is dead? AI is conquering mankind with art

变量的命名规则十二条

ORACLE列转行--某字段按指定分隔符转多行

Depth residual network

SEO学习的最好方式:搜索引擎

How to find a medical software testing institution? First flight software evaluation is an expert
随机推荐
Visitor tweets about how you can layout the metauniverse
How to configure GUI guide development environment
接口自动化测试框架:Pytest+Allure+Excel
The differences and advantages and disadvantages between cookies, seeion and token
C language_ Double create, pre insert, post insert, traverse, delete
LeetCode Algorithm 2181. 合并零之间的节点
Simple use of JWT
Missing monitoring: ZABBIX monitors the status of Eureka instance
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
Internal and external troubles of "boring ape" bayc
Raspberry pie serial port login and SSH login methods
Introduction to the basics of network security
【线上问题处理】因代码造成mysql表死锁的问题,如何杀掉对应的进程
What is the biggest problem that fresh e-commerce is difficult to do now
【Hot100】739. Daily temperature
[hot100] 739. Température quotidienne
#systemverilog# 可综合模型的结构总结
1189. Maximum number of "balloons"
Go learning --- use reflection to judge whether the value is valid
Interface automation test framework: pytest+allure+excel