当前位置:网站首页>大数相加
大数相加
2022-07-27 14:45:00 【Xavier丶】
算法总结:大数相加
我们在计算的过程中难免会遇到很大的数,而一个变量又无法将其存储,所以需要我们利用线性表来进行计算。
我们可以通过一道例题
1!+2!+3!+4!+…50!
int n,a[1000]={
0},b[1000]={
0}; //初始化数组使其便为零
scanf("%d",&n);
a[0]=b[0]=1; //将第一个元素作为1方便后面的乘法运算
for(int i=2;i<=n;i++){
for(int j=0;j<100;j++)
a[j]*=i;
//在a这个数组中计算每个数的阶乘
for(int j=0;j<100;j++)
if(a[j]>9){
a[j+1]+=a[j]/10;
a[j]%=10;
}
此部分为在计算每个部分的阶乘的数的结果
for(int j=0;j<100;j++){
b[j]+=a[j];
if(b[j]>9){
b[j+1]+=b[j]/10;
b[j]%=10;
}
}
此部分为计算每个阶乘结果的和
所以通过数组则可以计算比较大的数字以至于不会在单个变量中数据溢出。
边栏推荐
猜你喜欢

编码技巧——全局异常捕获&统一的返回体&业务异常

keil 采用 makefile 实现编译

2.2 basic elements of JMeter

: 0xc0000005: an access conflict occurs when writing position 0x01458000 - to be solved

Embedded development: tips and techniques -- seven techniques to meet the real-time deadline

时间序列——使用tsfresh进行分类任务

Penetration test - dry goods | 80 + network security interview experience post (interview)

判断数据的精确类型

Openwrt adds support for SD card

Nacos
随机推荐
4-digit random data
openwrt 增加RTC(MCP7940 I2C总线)驱动详解
测试新手学习宝典(有思路有想法)
Leetcode 226 flip binary tree (recursive)
First acquaintance with MySQL database
Addition, deletion, query and modification of MySQL table data
移动端h5常见问题
What is ti's calculation for successively canceling the agency rights of anfuli / Wenye / Shiping?
可载100人!马斯克发布史上最强“星际飞船” !最早明年上火星!
Web test learning notes 01
scrapy爬虫框架
Mapreduce实例(三):数据去重
三星关闭在中国最后一家手机工厂
快速高效删除node_modules
Constraints, design and joint query of data table -- 8000 word strategy + Exercise answers
Common problems of mobile terminal H5
ARIMA model selection and residuals
Security software related to wireless network analysis (airtrack ng)
DRF learning notes (preparation)
时间序列-ARIMA模型