题目坑不多,就是大数的求和,基本就是BigDecimal的翻版,但格式输出很坑,前面两个\n,最后一个\n,一直pe很难受。

解题思路:
把两个数字按字符的形式按位加起来,注意下进位和高低位就行了,和链表的加法实现基本一致的思路。

#include <string>
#include <sstream>
#include "iostream"using namespace std;// 反转字符串,保证低位相加,非必需
string reverse(string a) {int len = a.length();for (int i = 0; i < a.length() / 2; ++i) {char low = a[i];a[i] = a[len - 1 - i];a[len - 1 - i] = low;}return a;
}// 求和 注意'0'和0之间的编码差别
char calChar(char a, char b, int c) {return a + b + c - 2 * '0';
}char calChar(char a, int b) {return a + b - '0';
}string add(string a, string b) {int carry = 0;int bit = 0;string result = "";a = reverse(a);b = reverse(b);// 分两种情况讨论 ,a比b长,就以b为基准相加,剩余的位数直接补上去。b比a长,则相反if (a.length() >= b.length()) {for (int i = 0; i < b.length(); ++i) {bit = calChar(a[i], b[i], carry) % 10;carry = calChar(a[i], b[i], carry) / 10;result += bit + '0';}for (int i = b.length(); i < a.length(); ++i) {bit = calChar(a[i], carry) % 10;carry = calChar(a[i], carry) / 10;result += bit + '0';}} else {for (int i = 0; i < a.length(); ++i) {bit = calChar(a[i], b[i], carry) % 10;carry = calChar(a[i], b[i], carry) / 10;result += bit + '0';}for (int i = a.length(); i < b.length(); ++i) {bit = calChar(b[i], carry) % 10;carry = calChar(b[i], carry) / 10;result += bit + '0';}}if (carry > 0) result += carry + '0';return reverse(result);
}int main() {int times;string a, b, result;cin >> times;for (int i = 1; i <= times; i++) {cin >> a >> b;result += "Case " + to_string(i) + ":\n" + a + " + " + b + " = " + add(a, b);if (i < times) result += "\n\n";else result += "\n";}cout << result;return 0;
}

杭电oj 1002 c++版本相关推荐

  1. 【ACM】杭电OJ 1002

    题目链接:杭电OJ 1002 表面上看是a+b,很简单,其实是大数的相加,所以要考虑的因素有很多:(瞬间觉得要是能用Python多好,哈哈哈) 还有!!! 要特别注意格式的输出!!!真的害死人!!!摸 ...

  2. 杭电oj 1002 C语言解法

    杭电oj这道题的解法是要通过字符数组储存数字在进行运算的,同时涉及到数值进位的问题,以下是代提供码参考. #include<stdio.h> #include<string.h> ...

  3. 杭电OJ 1002 题目解答

    HDOJ 1002 A+B Problem Ⅱ AC 代码: #include <iostream> #include <string> #include <algori ...

  4. 杭电oj 1001 c++版本

    这个没啥说的,注意循环条件1开始 #include<iostream> using namespace std;int main(){int a, sum;string result;wh ...

  5. 杭电oj 1000 c++ 版本

    就是 a + b ,但坑在可能输入无效字符 #include<iostream> using namespace std;int main(){int a, b;while(cin > ...

  6. 赛马网ACM试题(原杭电oj ACM)java版答案(1000,10001,1002)

    赛马网ACM试题(原杭电OJ ACM试题)答案(java版) Author : Zhang Hailong   Date   : 2015-09-17    HomePage : http://  E ...

  7. 杭电OJ分类题目(1)

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(1) HDU Introduction HDU ...

  8. 【ACM】杭电OJ 2037

    题目链接:杭电OJ 2037 先把b[i]进行排序,然后,b[i]与a[i+1]进行比较. #include <iostream> #include <cstdio> #inc ...

  9. 【ACM】杭电OJ 2020(排序)

    题目链接:杭电OJ 2020 排序可以有冒泡排序,选择排序,或者直接调用函数. 下面是选择排序: #include <stdio.h> #include <math.h> in ...

最新文章

  1. 他676分考上清华却没微信,看到他的手机后大家沉默了
  2. Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍(转)
  3. java struts2 demo,Struts2第一个Demo求指导
  4. Android 自定义Action Button
  5. springcloud系列三 搭建服务模块
  6. 引用js_js值和引用
  7. 计算机网络 --- 数据链路层介质访问控制
  8. 微服务架构之「 访问安全 」
  9. python不合法的布尔表达式_python测试题
  10. 计蒜客挑战难题:最后一个单词的长度
  11. Django3与Vue3前后端分离搭建
  12. CentOS7下安装PostgresSQL9.4.4
  13. Linux安装nextcloud教程,WSL下安装nextcloud
  14. STK MATLAB联合仿真(一)STK与MATLAB的连接(COM与Connector)
  15. ENVI中计算植被覆盖指数
  16. opencv: 颜色通道 探究(图示+源码)
  17. 香蕉树上第十一根芭蕉——vs一些操作设置
  18. 自动光学检测(AOI)
  19. kali linux安装upupoo_Kali Linux 下载、引导、安装
  20. Windows API程序设计入门(新手的第一个Windows程序)

热门文章

  1. 浮动元素与兄弟之间的关系 速记 1211
  2. django-登陆案例-分析篇1909
  3. django-后台管理-笔记
  4. Ruby 101:重用、隐藏和多态
  5. HTML5文档查看器PrizmDoc发布v13.0,新增文档比较功能
  6. Http API笔记
  7. Symantec Backup Exec 2014 备份Exchange 2013之二安装主备服务器
  8. IntelliJ IDEA 如何从SVN导入项目 怎么部署 就是路劲和tomcat配置等等 步骤希望
  9. linux下安装php两种模式区别
  10. 周六——中国电影博物馆