博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDOJ-1002
阅读量:4592 次
发布时间:2019-06-09

本文共 2883 字,大约阅读时间需要 9 分钟。

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
Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
 

 大数相加的问题,开两个数组分别储存a、b,再开一个数组sum用来储存最终的和值。

这题要注意相加时储存的顺序和最后输出时的顺序不要搞错。

当然,这里特别强调一下最后的输出格式,最后一组数据只要换一行!一行!行!原谅愚蠢如我在这个点被坑了5次!!还是刷题太少。。

 

附AC代码

1 #include
2 #include
3 const int MAX=1050; 4 int main() 5 { 6 int n,m,len1,len2,maxlen,minlen; 7 char a[MAX],b[MAX],sum[MAX];//三个主要数组 8 char *max, *min; 9 scanf("%d",&n);10 for(m=1;m<=n;m++)11 {12 int i,j,flag=0;13 memset(sum,0,sizeof(sum));14 scanf("%s %s",&a,&b);15 len1=strlen(a);16 len2=strlen(b);17 if(len1>=len2)//判断长度,长的为max 18 {19 maxlen=len1;20 minlen=len2;21 max = a;22 min = b;23 }24 else25 {26 maxlen=len2;27 minlen=len1;28 max = b;29 min = a;30 }31 for(i=maxlen-1,j=minlen-1;i>=maxlen-minlen&&j>=0;i--,j--)//长度重叠部分相加 32 {33 sum[i]+=max[i]+min[j]-'0'-'0';//转换为十进制整数,下面的同理 34 if(i==0&&sum[0]>9)//如果两数组长度相等 35 {36 flag=1;//加在sum前表示进的1 37 sum[i]-=10;//本大于9,进1故减10 38 }39 else if(sum[i]>9)40 {41 sum[i-1]++;//进1 42 sum[i]-=10;43 }44 }45 for(i=maxlen-minlen-1;i>=0;i--)//剩下的max加到sum里 46 {47 sum[i]+=max[i]-'0';48 if(i==0&&sum[0]>9)49 {50 flag=1;51 sum[i]-=10;52 }53 else if(sum[i]>9)54 {55 sum[i-1]++;56 sum[i]-=10;57 }58 }printf("Case %d:\n",m);59 60 if(flag) 61 {62 63 printf("%s + %s = 1",a,b);}64 else65 {66 67 printf("%s + %s = ",a,b);68 }69 for(i=0;i

 

转载于:https://www.cnblogs.com/Kiven5197/p/5460254.html

你可能感兴趣的文章
make报错make: *** [sapi/cli/php] Error 1
查看>>
关于Java中~的问题
查看>>
[C/C++语言标准] ISO C99/ ISO C11/ ISO C++11/ ISO C++14/ISO C++17 Downloads
查看>>
44.Linux君正X1000-添加st7789v显示
查看>>
AC日记——大小写字母互换 openjudge 1.7 14
查看>>
第二届长三角音视频技术交流会筹备中
查看>>
关于启动Tomcat乱码问题的解决
查看>>
ASC7 Problem G. Network Wars
查看>>
js刷新页面方法大全
查看>>
java html中文汉字 反转义
查看>>
(4)HTML 5提供的一些新的标签用法以及和HTML 4的区别
查看>>
微软MSDN论坛上海聚会
查看>>
32进程调用64dll的解决方法
查看>>
MySQL 1071错误解决办法
查看>>
Linux 硬盘工具之hdparm
查看>>
《Java虚拟机规范》阅读(一):简介和Java虚拟机结构
查看>>
spring3.2.5学习(二)——IoC注解配置
查看>>
牛客网 牛客小白月赛2 D.虚虚实实-无向图判欧拉路径
查看>>
Visual C++界面编程技术随书代码
查看>>
OpenCV编程案例:最大熵阈值分割算法实现
查看>>