当前位置:网站首页>1151: 大整数加法
1151: 大整数加法
2022-04-21 09:06:00 【谦QIAN】
1151: 大整数加法
题目描述
比利经常会碰到超大整数的加法运算,而普通的计算器上无法进行。因此他想你帮他写一个程序来计算结果。
输入
输入数据有多组。首先输入一个整数T,表示有T组输入。
每组输入两个大整数,并用空格隔开。每个整数最多1000位。没有负数输入。
输出
对于每组输入,输出两个整数的和,单独占一行。
样例输入 Copy
2
1 2
112233445566778899 998877665544332211
样例输出 Copy
3
1111111111111111110
来源/分类
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define N 1001
/* 大整数加法,以字符串的形式进行 先逆序,这样方便从个位计算 a,b,c,r数组分别表示加数,加数,对和求余,进位 */
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);
//结果的位数<=两者较大的那个
if(m<n) max=n;
else max=m;
//每次都要置为0
for(int i=0;i<=max;i++){
a[i]=b[i]=c[i]=r[i]=0;
}
//将字符串逆序且将字符型改为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++){
//直接+c[i],再求余数,并保存在r[i]
r[i]=(a[i]+b[i]+c[i])%10;
//若有进位,c[i+1]就是从0改为进位数
if((a[i]+b[i]+c[i])/10!=0) c[i+1]=(a[i]+b[i]+c[i])/10;
}
//最后一位不一定有进位,即c[max]有可能为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]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44500344/article/details/107868465
边栏推荐
- 1163: 亲和串(字符串)
- JS原型与原型链
- Getting started with object detection FAQs (deep learning / image classification)
- Redisson introduction and integration
- Opencv -- separate color channel, image contrast, brightness adjustment, discrete Fourier transform (10)
- 2022化工自动化控制仪表考试练习题及模拟考试
- ARPU, the user of China Mobile, has re entered the rising channel, and the salary of employees has also increased steadily
- Penetration practice - dig a school site vulnerability (APP vulnerability)
- YApi基本使用(2022-04-15)
- A system has n processes with a total of 7 resources. Each process needs 3 resources. What is the maximum number of N without deadlock? (analysis attached)
猜你喜欢

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

L2-026 small generation (25 points)

Drafting and Revision: Laplacian Pyramid Network for Fast High-Quality Artistic Style Transfer--T Li

LDO系列--PSRR

笔记0104. MySQL 高级 - 索引 - 概述
![BUUCTF[ACTF2020 新生赛]Include](/img/20/3c0d0286c869385ee0c9a119e337e2.png)
BUUCTF[ACTF2020 新生赛]Include

Penetration test - roaming from public cloud to intranet rce deserialization FRP
![Buuctf [geek challenge 2019] easysql](/img/35/84422c6aea0c96f6cbcf8354729604.png)
Buuctf [geek challenge 2019] easysql

Open3d读写pcd点云文件

卷积运算与互相关运算
随机推荐
L2-031 深入虎穴 (25 分)
C 100 points secret script sduwh
JS原型与原型链
Open3d读写pcd点云文件
2022年上海市安全员C证考试模拟100题及模拟考试
1164: 字符串加密
Actual combat analysis of PC wechat robot personal number interface API wechat agrees with friend call
SurfaceView高性能绘制(四)代码实践篇-绘制多张图片
Worker man passes parameters to the method in timer
Maya basic tutorial and basic operation explanation
Penetration practice - no echo rce thinkphp5 getshell
The database has a table in which JSON data is stored in one field. I need to parse this string when this table is updated, and then update the parsed JSON data to another table. What are the good sug
卷积运算与互相关运算
某系统拥有N个进程,总共7个资源,每个进程需要3个资源,问N数量最多为多少不会死锁?(附解析)
Handler异步消息传递机制(二)在子线程中创建Handler
Controlled and uncontrolled components
Getting started with object detection FAQs (deep learning / image classification)
ARPU, the user of China Mobile, has re entered the rising channel, and the salary of employees has also increased steadily
2022年主要的编程语言及应用
Notes of the most complete grain mall in the whole network_ 02. Introduction to the overall effect of the project (2022-04-02)