PAT A 1001. A+B Format (20)
来源:互联网 发布:linux shell 等待 编辑:程序博客网 时间:2024/04/30 04:54
Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
Input
Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.
Output
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
Sample Input-1000000 9Sample Output
-999,991
// 题目分析:
没有难点,可以有多种实现方法,此处练习堆栈操作
// 代码
#include <iostream>#include <string>#include <stack>using namespace std;int main(){int a,b;cin>>a>>b;int c=a+b;if(c<0){c=-c;cout<<'-';}else if(c==0){cout<<0;return 0;}stack<char> s;int count=0;//计数器while(c!=0){if(count%3==0&&count!=0){s.push(',');}s.push(c%10+'0');count++;c/=10;}while(!s.empty()){cout<<s.top();s.pop();}system("pause");return 0;}
// 结果
0 0
- PAT A 1001. A+B Format (20)
- PAT-A-1001. A+B Format (20)
- PAT A 1001. A+B Format (20)
- PAT-A 1001. A+B Format (20)
- PAT-A 1001. A+B Format (20)
- Pat(A) 1001. A+B Format (20)
- PAT-A 1001. A+B Format (20)
- 【PAT】1001. A+B Format (20)
- PAT:1001. A+B Format (20)
- ( PAT )1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- [PAT]1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- 如何为嵌入式开发建立交叉编译环境
- js 触发事件种类类型有哪些?
- OllyScript脚本练习
- AtCoder Regular Contest 069 D - Menagerie 枚举起点 递推
- lightoj 1381 - Scientific Experiment dp
- PAT A 1001. A+B Format (20)
- 【读论文】node2vec: Scalable Feature Learning for Networks
- 浅析 Linux 中的时间编程和实现原理,第 1 部分: Linux 应用层的时间编程
- 第一章 计算机网络概述
- Leetcode-Algorithms Number Complement(数字补码)
- Linux 初始 RAM 磁盘(initrd)概述
- html内容整理第一章节
- listen函数和accept函数
- 《Docker……》笔记-8 Docker核心技术