当前位置:网站首页>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;
}
边栏推荐
- A brief introduction of reverseme in misc in the world of attack and defense
- Cookie Technology & session Technology & ServletContext object
- Multi attribute object detection on rare aircraft data sets: experimental process using yolov5
- Yield method of tread
- 升级版手机检测微信工具小程序源码-支持多种流量主模式
- Thought map of data warehouse construction
- 1091: two or three things in childhood (multi instance test)
- C - Inheritance - hidden method
- 树莓派3B更新vim
- Proteus -- Serial Communication parity flag mode
猜你喜欢
Win10 64 bit Mitsubishi PLC software appears oleaut32 DLL access denied
Misc of BUU (update from time to time)
配置树莓派接入网络
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
呆错图床系统源码图片CDN加速与破解防盗链功能
leetcode704. 二分查找(查找某个元素,简单,不同写法)
Go learning --- use reflection to judge whether the value is valid
1189. Maximum number of "balloons"
数据仓库建设思维导图
随机推荐
mysql如何合并数据
Solution to the problem of breakthrough in OWASP juice shop shooting range
What is the biggest problem that fresh e-commerce is difficult to do now
Visitor tweets about how you can layout the metauniverse
ORACLE列转行--某字段按指定分隔符转多行
【JDBC】快速入门教程
C - Inheritance - hidden method
19. Actual memory management of segment page combination
UWA pipeline version 2.2.1 update instructions
The psychological process from autojs to ice fox intelligent assistance
L'auteur est mort? Ai utilise l'art pour conquérir l'humanité
Lesson 12 study notes 2022.02.11
MPLS experiment
BIO模型实现多人聊天
SSM learning
Cif10 actual combat (resnet18)
Three methods of adding color to latex text
[JDBC] quick start tutorial
这个高颜值的开源第三方网易云音乐播放器你值得拥有
Short video, more and more boring?