programming-challenges Primary Arithmetic (110501) 题解
2015-07-20 14:39
162 查看
这是一道明显的水题,必须过的。
#include <iostream> #include <sstream> #include <fstream> #include <string> #include <vector> #include <queue> #include <map> #include <set> #include <stack> #include <assert.h> #include <algorithm> #include <math.h> #include <ctime> #include <functional> #include <string.h> #include <stdio.h> #include <numeric> #include <float.h> using namespace std; int main() { string s1, s2; while (cin >> s1 >> s2) { if (s1 == "0" && s2 == "0") break; bool carrier = false; int i1 = s1.size() - 1, i2 = s2.size() - 1; int ans = 0; while (i1 >= 0 || i2 >= 0) { if (carrier) ans++; int val1 = 0, val2 = 0; if (i1 >= 0) val1 = s1[i1] - '0'; if (i2 >= 0) val2 = s2[i2] - '0'; int val = val1 + val2; if (carrier) val++; if (val > 9) carrier = true; else carrier = false; i1--; i2--; } if (carrier) ans++; if (ans == 0) cout << "No carry operation." << endl; else if (ans == 1) cout << "1 carry operation." << endl; else cout << ans << " carry operations." << endl; } return 0; }
相关文章推荐
- iOS多线程开发(四)---线程同步
- 解决安装AppServ问题(转)
- 有关MAC、PHY和MII
- JDBC读取BLOB类型
- 说好一个创业故事的5个步骤
- android studio导入eclipse或者 android studio工程文件的超简技巧
- 优化之误!
- 关于FastStone Capture输入中文出现乱码.
- 检测密码强度
- C/C++的一些备忘
- HDU2044 一只小蜜蜂...
- leetcode[107]:Binary Tree Level Order Traversal II
- 分享2个android开源库 DanmakuFlameMaster以及Volley
- 布尔表达式和选择结构
- 用jquery判断输入框还可以输入多少字
- iOS中url的特殊字符转换
- “英语”哪有男没用?
- Mac下配置JAVA_HOME
- 一种基于PTP 协议的局域网高精度时钟同步方法(转)
- Android - ViewPager使用PagerAdapter,instantiateItem时出现IndexOutOfBoundsException