HDU 1002.A + B Problem II-数组模拟-大数相加
2024-08-26 20:56:49
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 435932 Accepted Submission(s): 84825
Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
112233445566778899 + 998877665544332211 = 1111111111111111110
Author
Ignatius.L
代码:
#include<stdio.h>
#include<string.h>
int main(){
char a[],b[];
int c[],d[],ans[];
int n,i,k,j,h,t,l;
int len1,len2;
scanf("%d",&n);
h=;
while(n--){
h+=;
for(i=;i<;i++){
c[i]=d[i]=;
ans[i]=;
}
scanf("%s %s",&a,&b);
len1=strlen(a);
len2=strlen(b);
for(i=;i<len1;i++){
c[i]=a[i]-'';
}
for(i=;i<len2;i++){
d[i]=b[i]-'';
}
printf("Case %d:\n",h);
for(i=;i<len1;i++)
printf("%d",c[i]);
printf(" + ");
for(i=;i<len2;i++)
printf("%d",d[i]);
printf(" = ");
for(i=,j=len1-;i<len1/;i++,j--){
t=c[j];
c[j]=c[i];
c[i]=t;
}
for(i=,j=len2-;i<len2/;i++,j--){
t=d[j];
d[j]=d[i];
d[i]=t;
}
l=len1>len2?len1:len2;
for(i=,k=;i<=l;i++,k++){
if(c[i]+d[i]<)
ans[k]=c[i]+d[i];
else{
ans[k]=(c[i]+d[i])%;
c[i+]+=(c[i]+d[i])/;
} }
for(j=k-;j>=;j--){
if(j==k-&&ans[j]==)
continue;
printf("%d",ans[j]);
}
if(n==)
printf("\n");
else
printf("\n\n");
}
return ;
}
最新文章
- 高效 Java Web 开发框架 JessMA v3.3.1 正式发布
- mac 下搭建php 编程环境全过程
- 【elasticsearch】python下的使用
- BZOJ3570 : DZY Loves Physics I
- The Separator in Grid_BFS
- Java 快排
- Mac下安装 MongoDB
- highchart
- 24 服务AIDL
- 浏览器仿EXCEL表格插件 版本更新 - 智表ZCELL产品V1.3.2更新
- 理论铺垫:阻塞IO、非阻塞IO、IO多路复用/事件驱动IO(单线程高并发原理)、异步IO
- Python字符串与格式化的一点用法
- vim编辑器的设置
- 微信小程序 table 简单测试
- Ext Js 6.2.1 classic grid 滚动条bug解决方案
- java ---->; 注解/反射
- 修改Linux下的文件以及文件夹的权限
- 星系炸弹-2015省赛C语言A组第二题
- jar 命令详解
- java后台读取/解析 excel表格