当前位置:网站首页>1151: large integer addition
1151: large integer addition
2022-04-21 09:10:00 【Qian Qian】
1151: Large integer addition
Title Description
Billy often encounters the addition of very large integers , And ordinary calculators can't do . So he wants you to help him write a program to calculate the result .
Input
Multiple groups of input data . First, enter an integer T, Express T Group input .
Enter two large integers for each group , And separated by spaces . Each integer has a maximum of 1000 position . No negative number input .
Output
For each group of input , Output the sum of two integers , Take a line alone .
The sample input Copy
2
1 2
112233445566778899 998877665544332211
Sample output Copy
3
1111111111111111110
source / classification
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define N 1001
/* Large integer addition , In the form of string In reverse order , This makes it easy to calculate from individual bits a,b,c,r Arrays represent addends, respectively , Addition number , Sum and remainder , carry */
int main(){
int t,m,n,max=0;
scanf("%d",&t);
char s1[N],s2[N];
int a[N],b[N],c[N],r[N];
while(t--){
scanf("%s%s",&s1,&s2);
m=strlen(s1);
n=strlen(s2);
// Number of digits of the result <= The bigger one
if(m<n) max=n;
else max=m;
// Set it to... Every time 0
for(int i=0;i<=max;i++){
a[i]=b[i]=c[i]=r[i]=0;
}
// Reverse the string and change the character type to int
for(int i=0;i<m;i++){
a[m-i-1]=s1[i]-'0';
}
for(int i=0;i<n;i++){
b[n-i-1]=s2[i]-'0';
}
for(int i=0;i<=max;i++){
// direct +c[i], Find the remainder , And keep it in r[i]
r[i]=(a[i]+b[i]+c[i])%10;
// If there is carry ,c[i+1] It's from 0 Change to carry number
if((a[i]+b[i]+c[i])/10!=0) c[i+1]=(a[i]+b[i]+c[i])/10;
}
// The last bit does not necessarily have a carry , namely c[max] It could be 0
if(r[max]!=0) printf("%d",r[max]);
for(int i=max-1;i>=0;i--){
printf("%d",r[i]);
}
printf("\n");
}
return 0;
}
版权声明
本文为[Qian Qian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210906132156.html
边栏推荐
- 2022茶艺师(初级)考题及在线模拟考试
- ARPU, the user of China Mobile, has re entered the rising channel, and the salary of employees has also increased steadily
- 1148: 组合三位数之一
- Surfaceview high performance rendering (V) code practice - let the drawn picture move
- Intranet penetration - proxy penetration - rights lifting - injection - MSF Middleware - domain penetration - log clearing - learning resources
- Add log4j log function
- Actual combat penetration - fofa dirbrute - code audit - construction POC ueditor - decryption - WAF Godzilla
- Pipy mqtt agent (III) logging
- L2-026 small generation (25 points)
- LDO series -- PSRR
猜你喜欢

方格分割【dfs】

Yapi basic use (2022-04-15)

2022 Shandong Province safety officer C certificate examination questions and simulation examination

Geek challenge 2019 upload 1

Notes of the most complete grain mall in the whole network_ 02. Introduction to the overall effect of the project (2022-04-02)

CC00043.CloudJenkins—————————————

LDO series -- PSRR

Pipy MQTT 代理之(三)Logging

Detailed explanation of native and H5 mixed development
Open3d读写ply点云文件
随机推荐
Open3d reads and writes PCD point cloud files
YApi基本使用(2022-04-15)
【ACM】131. Split palindrome string
Drafting and Revision: Laplacian Pyramid Network for Fast High-Quality Artistic Style Transfer--T Li
Map Object WeakMap
SurfaceView高性能绘制(四)代码实践篇-绘制多张图片
[MySQL] Based on linux-centos7 9 detailed installation tutorial
ShardingSphere简介
Remove linked list elements < difficulty coefficient >
2022 chemical automation control instrument examination exercises and simulation examination
My blog navigation directory (constantly sorting and updating...)
Controlled and uncontrolled components
1155: 字符串比较 多实例
[(strongly pushed) Li Hongyi 2021 / 2022 spring machine learning course] unsupervised learning - linear methods
Implementation of Apache Doris rapid deployment operation and maintenance guide based on ansible
批量处理数据对比(<foreach>标签和sqlsession)
1161: 字符串长度(指针专题)
2022 tea artist (primary) examination questions and online simulation examination
最新系统漏洞--OMERO.web跨站脚本漏洞
Template adaptation guide for toudabi thesis