博客
关于我
<hdu - 1002> A + B Problem II
阅读量:559 次
发布时间:2019-03-10

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

 杭电hdu上的链接

 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的题也难不到哪去,谁知道改了好几个小时。开始试了int,不行,又改long,不行,然后改long long,还是不行。只能另外想一种方法了:用大位数加法,用数组装数字,然后从个位开始逐位相加,过十进一。开始的时候想肯定是用数组了,肯定不会是字符数组。但是做着做着发现不行,数组的长度无法确定,想起来字符数组有个strlen的用法,就改用字符数组了。
 以下是具体代码:
1 #include 
2 #include
3 #include
4 #define N 1005 5 using namespace std; 6 7 int count = 0; 8 int a[N]; 9 int b[N]; 10 int he[N]; 11 char sza[N]; 12 char szb[N]; 13 14 void calcuation() { 15 int flag = 1; 16 int len1,len2; 17 len1 = strlen(sza); 18 len2 = strlen(szb); 19 int i,j,k,l; 20 i = 0; 21 while(sza[i] != '\0') { 22 a[i] = sza[i] - '0'; 23 i++; 24 } 25 i = 0; 26 while(szb[i] != '\0') { 27 b[i] = szb[i] - '0'; 28 i++; 29 } 30 if(len1 < len2) {l = len1-1;flag = 0;} 31 if(len1 > len2) {l = len2-1;flag = -1;} 32 if(len1 == len2) l = len1-1; 33 k = 0; 34 int num = 0; 35 int m,n; 36 m=len1-1; 37 n=len2-1; 38 while(l >= 0) { 39 he[k] += a[m--] + b[n--]; 40 41 if(he[k] >= 10) { 42 num = he[k] / 10; 43 he[k] = he[k] % 10; 44 he[k+1] = num; 45 num = 0; 46 } 47 k++; 48 l--; 49 } 50 i = k; 51 j = k; 52 if(flag == 0) {//len1 < len2 53 for(k = len2-1-i;k >= 0; k--) { 54 he[j] += b[k]; 55 j++; 56 } 57 } 58 if(flag == -1) {//len1 > len2 59 for(k = len1-1-i;k >= 0; --k) { 60 he[j] += a[k]; 61 j++; 62 } 63 } 64 } 65 66 void find_print() { 67 int loc; 68 calcuation(); 69 printf("Case %d:\n",count); 70 // cout << "Case";//此处考虑到用cout输出需要的行数多,故使用printf输出 71 // cout << count; 72 // cout << ":\n"; 73 int i; 74 i = 0; 75 while(sza[i]!='\0') { 76 printf("%c",sza[i]); 77 i++; 78 } 79 cout << " + "; 80 i = 0; 81 while(szb[i]!='\0') { 82 printf("%c",szb[i]); 83 i++; 84 } 85 cout << " = "; 86 for(i = N; i >= 0; i--) { 87 if(he[i] != 0) { 88 loc = i; 89 break; 90 } 91 } 92 for(i = loc;i >= 0; i--) { 93 printf("%d",he[i]); 94 } 95 cout << endl; 96 } 97 98 int main() { 99 int n;100 cin >> n;101 while(n--) {102 count++;103 memset(he,0,sizeof(he));104 memset(a,0,sizeof(a));105 memset(b,0,sizeof(b));106 cin >> sza >> szb;107 find_print();108 if(n != 0)///根据题意最后用换行109 cout << endl;110 }111 return 0;112 }
View Code

 

  方法仅供参考,我知道肯定不是最简单的方法,有时间会再改善的,谢谢码友支持,欢迎评论。

转载地址:http://pvwvz.baihongyu.com/

你可能感兴趣的文章
[系列] Go gRPC 调试工具
查看>>
django-表单之模型表单渲染(六)
查看>>
c++之程序流程控制
查看>>
一位年轻而优秀的.NET开发者的成长点滴
查看>>
如何使用ABP进行软件开发(1) 基础概览
查看>>
AlwaysOn配置时在连接步骤时报错(35250)
查看>>
排序算法之总结
查看>>
微软云Linux服务器 Mysql、tomcat远程连接错误解决办法
查看>>
Python数据分析(二): Numpy技巧 (2/4)
查看>>
09 . Python3之常用模块
查看>>
Python学习笔记-StatsModels 统计回归(3)模型数据的准备
查看>>
Velocity.js初步
查看>>
yarn出现“There are no scenarios ; must have at least one"
查看>>
nginx上配置phpmyadmin
查看>>
HustOJ二次开发之修改数据库连接池
查看>>
redis集群报错:(error) CLUSTERDOWN Hash slot not served
查看>>
一个JAVA应用启动缓慢问题排查 --来自jdk securerandom 的问候
查看>>
spring-boot-2.0.3之redis缓存实现,不是你想的那样哦!
查看>>
httprunner学习23-加解密
查看>>
有道云笔记 同步到我的博客园
查看>>