题目链接:http://www.patest.cn/contests/pat-a-practise/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

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 9

Sample Output

-999,991

题意大意:

输出a+b,以每三位加一个逗号的格式。

解题:

注意后面部分假设小于100需加前缀0。

代码:

#include <cstdio>
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;
int main()
{
int a,b,ans;
cin>>a>>b;
ans=a+b;
if(ans<0)
{
cout<<"-";
ans=-ans;
}
if(ans<1000)
cout<<ans<<endl;
else if(ans<1000000)
cout<<ans/1000<<","<<fixed<<setw(3)<<setfill('0')<<ans%1000<<endl;
else
cout<<ans/1000000<<","<<fixed<<setw(3)<<setfill('0')<<ans/1000-ans/1000000*1000<<","<<fixed<<setw(3)<<setfill('0')<<ans%1000<<endl;
return 0;
}

PAT-PAT (Advanced Level) Practise 1001. A+B Format (20) 【二星级】的更多相关文章

  1. PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642 题目描述: Calculate a+b and output the sum i ...

  2. PAT (Advanced Level) Practise 1001 解题报告

    GiHub markdown PDF 问题描述 解题思路 代码 提交记录 问题描述 A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判 ...

  3. PAT (Advanced Level) Practice 1001 A+B Format (20 分)

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 Calculate a+b and ...

  4. PAT (Advanced Level) Practice 1001 A+B Format 分数 20

    Calculate a+b and output the sum in standard format -- that is, the digits must be separated into gr ...

  5. PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...

  6. PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...

  7. PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642 题目描述: With the 2010 FIFA World Cu ...

  8. PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642 题目描述: Given a non-negative integer N ...

  9. PAT 甲级 1001 A+B Format (20)(20 分)

    1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...

随机推荐

  1. centos7 install vim8

    centos7 install vim8 Git and dependency Git: https://github.com/vim/vim # yum install -y perl-devel ...

  2. spring中bean的配置详解--定义parent

    在工作中碰到了好多的配置文件,具体来说是spring 中bean配置的parent的配置,搞的我一头雾水,仔细看一下spring中有关bean的配置,剖析一下,具体什么含义! 一.Spring IoC ...

  3. 苹果平台上的媒体流播放技术HLS

    近日在和朋友聊起媒体流的服务器端实时转码技术的时候,发现苹果的各种终端上的视频播放并未使用常见的基于UDP的RTSP/RTP,而强制使用了Http Live Stream技术,这里稍稍总结了如下. 苹 ...

  4. LeetCode(26) Remove Duplicates from Sorted Array

    题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  5. C# 禁止通过标题栏移动窗体

    protected override void WndProc(ref Message m) { base.WndProc (ref m); if(m.Msg == 0x84) //不让拖动标题栏 { ...

  6. 异常 Failed to bind NettyServer on /10.133.7.216:29105, cause: Failed to bind to: /0.0.0.0:29105

    "C:\Program Files\Java\jdk1.7.0_80\bin\java" -agentlib:jdwp=transport=dt_socket,address=12 ...

  7. jquery取当前节点的上级ID

  8. HDU2069-Coin Change

    Coin Change 这题题意和UVA674很像,但加了一个限制条件不能超过100个硬币.于是我们可以用d[i][j]来表示硬币数量为i时金钱总数为j时的方法总数,总钱不能超过250. const ...

  9. 洛谷P1759 通天之潜水

    题目背景 直达通天路·小A历险记第三篇 题目描述 在猴王的帮助下,小A终于走出了这篇荒山,却发现一条波涛汹涌的河拦在了自己的面前.河面上并没有船,但好在小A有n个潜水工具.由于他还要背重重的背包,所以 ...

  10. 细胞分裂(洛谷 P1069)

    题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家.现在,他正在为一个细胞实 验做准备工作:培养细胞样本. Hanks 博士手里现在有 N 种细胞,编号从 1~N,一个 ...