博客
关于我
<hdu - 1002> A + B Problem II
阅读量:575 次
发布时间: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/

你可能感兴趣的文章
MySQL 大数据量快速插入方法和语句优化
查看>>
mysql 如何给SQL添加索引
查看>>
mysql 字段区分大小写
查看>>
mysql 字段合并问题(group_concat)
查看>>
mysql 字段类型类型
查看>>
MySQL 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>
Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
查看>>
Mysql 学习总结(89)—— Mysql 库表容量统计
查看>>
mysql 实现主从复制/主从同步
查看>>
mysql 审核_审核MySQL数据库上的登录
查看>>
mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
查看>>
mysql 导入导出大文件
查看>>
MySQL 导出数据
查看>>