PTA PAT (Advanced Level) Practice-1001

编程入门 行业动态 更新时间:2024-10-28 11:31:21

PTA PAT (<a href=https://www.elefans.com/category/jswz/34/1762457.html style=Advanced Level) Practice-1001"/>

PTA PAT (Advanced Level) Practice-1001

1001 A+B Format (20 分)
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 Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −10
​6
​​ ≤a,b≤10
​6
​​ . The numbers are separated by a space.

Output Specification:
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 9
Sample Output:
-999,991

思路:用栈数据结构就行了,还有就是注意a + b等于零的时候要输出0

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<stack>
using namespace std;
stack<char> p;
int main()
{int a, b, t = 1, flag = 0;cin >> a >> b;if(a+b < 0){flag = 1;}a = abs(a+b);if(a == 0){cout << "0";}else{while(a != 0){char c = a%10 +'0';p.push(c);if(t%3 == 0 && a/10 != 0){p.push(',');}t++;a /= 10;}}if(flag)cout << "-";while(!p.empty()){printf("%c",p.top());p.pop();}return 0;
}

更多推荐

PTA PAT (Advanced Level) Practice-1001

本文发布于:2023-07-28 17:50:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1267559.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Advanced   PAT   PTA   Practice   Level

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!