博客
关于我
A + B Problem II HDU - 1002(简单string)
阅读量:173 次
发布时间:2019-02-28

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

#include 
#include
#include
using namespace std;string sum(string a, string b){ if(a.length() < b.length()) { string tmp = a; a = b; b = tmp; } string tmp = ""; for(int i = 0; i < a.length()-b.length(); i++) tmp += "0"; b = tmp + b; //以上操作都是对齐操作。 int x; bool flag = false; string ans = ""; for(int i = a.length()-1; i >= 0; i--) { x = a[i] - '0' + b[i] - '0' + flag; if(x >= 10) { flag = true; ans += '0' + x - 10; } else { flag = false; ans += x + '0'; } } if(flag) //注意这里,比如99 + 11, 和是3位, ans += "1"; reverse(ans.begin(), ans.end()); return ans;}int main(){ string str1, str2, ans; int n; cin >> n; for(int i = 1; i <= n; i++) { cin >> str1 >> str2; cout << "Case " << i << ":" << endl; cout << str1 << " + " << str2 << " = " << sum(str1, str2) << endl; if(i != n) cout << endl; } return 0;}

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

你可能感兴趣的文章
Nacos2.X源码分析:服务注册、服务发现流程
查看>>
NacosClient客户端搭建,微服务注册进nacos
查看>>
Nacos中使用ribbon
查看>>
Nacos使用OpenFeign
查看>>
Nacos使用Ribbon
查看>>
Nacos做注册中心使用
查看>>
Nacos做配置中心使用
查看>>
Nacos入门过程的坑--获取不到配置的值
查看>>
Nacos原理
查看>>
Nacos发布0.5.0版本,轻松玩转动态 DNS 服务
查看>>
Nacos启动异常
查看>>
Nacos命名空间配置_每个人用各自自己的命名空间---SpringCloud Alibaba_若依微服务框架改造---工作笔记001
查看>>
Nacos和Zookeeper对比
查看>>
Nacos在双击startup.cmd启动时提示:Unable to start embedded Tomcat
查看>>
Nacos基础版 从入门到精通
查看>>
Nacos如何实现Raft算法与Raft协议原理详解
查看>>
Nacos安装教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
Nacos实战攻略:从入门到精通,全面掌握服务治理与配置管理!(上)
查看>>
Nacos实战攻略:从入门到精通,全面掌握服务治理与配置管理!(下)
查看>>
Nacos心跳机制实现快速上下线
查看>>