当前位置:网站首页>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;
}
边栏推荐
- ROS learning_ Basics
- 巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...
- MPLS experiment
- Yield method of tread
- Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
- Cif10 actual combat (resnet18)
- First knowledge of OpenGL es learning (1)
- Fast target recognition based on pytorch and fast RCNN
- 多线程和并发编程(二)
- Refer to how customer push e-commerce does content operation
猜你喜欢
杰理之如若需要大包发送,需要手机端修改 MTU【篇】
Thought map of data warehouse construction
Internal and external troubles of "boring ape" bayc
WPF之MVVM
Multi attribute object detection on rare aircraft data sets: experimental process using yolov5
hydra常用命令
leetcode6109. 知道秘密的人数(中等,周赛)
UWA pipeline version 2.2.1 update instructions
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
配置树莓派接入网络
随机推荐
Librosa audio processing tutorial
First knowledge of OpenGL es learning (1)
leetcode1020. Number of enclaves (medium)
Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file
Crawling exercise: Notice of crawling Henan Agricultural University
How to find a medical software testing institution? First flight software evaluation is an expert
Simple use of JWT
JDBC学习笔记
树莓派3B更新vim
Introduction to ros2 installation and basic knowledge
Seriously recommend several machine learning official account
CDN acceleration and cracking anti-theft chain function
Entity Developer数据库应用程序的开发
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
Cookie技术&Session技术&ServletContext对象
可变参数重载时的内存错误
MVVM of WPF
OpenJudge NOI 2.1 1749:数字方格
PCL realizes frame selection and clipping point cloud
SEO学习的最好方式:搜索引擎