PAT (Advanced Level) Practice 1001 A+B Format (20 分)
2024-08-28 03:10:00
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400
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 −. 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
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
int main()
{
int n,m,sum;
while(cin>>n>>m){
sum=n+m;
if(sum==){
cout<<<<endl;
continue;
}
int flag=;
if(sum<){
flag=;
sum=-sum;
}
char c='-';
stack<char> s;
int t=;
while(sum){
c=sum%+'';
s.push(c);
t++;
if(t%==&&sum/) s.push(',');
sum/=;
}
if(flag) s.push('-');
while(!s.empty()){
cout<<s.top();
s.pop();
}
cout<<endl;
}
return ;
}
最新文章
- linux 学习随笔-shell基础知识
- 在不同的pyhon版本中切换
- CDN(内容分发网络)是什么?
- iOS - UIButton
- C++记录debug信息的log类
- 让你了解x86的中断
- Java Servlet-http协议
- wps 2012-2013 通杀漏洞(CVE-2013-3934)
- phpcms(1)phpcms V9 MVC模式 与 URL访问解析(转)
- 基于HttpClient 4.3的可訪问自签名HTTPS网站的新版工具类
- 第一章 C++基本认识
- Gradle构建Java工程配置详解
- python学习笔记1-python相关应用套件
- 搭建、访问ftp
- android组件之TabHost
- 设计模式 笔记 单例模式 Singleton
- WPF为stackpanel设置滚动条
- css样式冲突问题练习例子
- RSA加密的测试demo
- plotplayer声道设置原声