题目链接: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. Python Syntax Summary

    # _*_ coding: utf-8 _*_ """########################################################## ...

  2. css--字体和文本样式

    文字样式属性 字体:font-family 文字大小:font-size 文字颜色:font-color 文字粗细:font-weight 文字样式:font-style font-family字体属 ...

  3. margin塌陷和margin合并问题及解决方案

    margin塌陷 先举个例子 <style> body{ background-color:#000; } .wrapper{ width:200px; height:200px; bac ...

  4. 「 Luogu P2420 」 让我们异或吧

    # 解题思路 两点之间的路径的话一定经过它们两个 LCA,这一点已经是显而易见的,那么再来看看异或的性质. $$a\ xor\ b\ xor\ b = a\\ a\ xor\ a=0\\ a\ xor ...

  5. scanf_s获取参数,清空缓冲区,判断是否读取成功

    #include<stdio.h> int main() { ]; ) { printf("Please input:\n"); ); ) { printf(" ...

  6. LeetCode(74) Search a 2D Matrix

    题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the fo ...

  7. manjaro xfce 18.0 踩坑记录

    manjaro xfce 18.0 踩坑记录 1 简介1.1 Manjaro Linux1.2 开发桌面环境2 自动打开 NumLock3 系统快照3.1 安装timeshift3.2 使用times ...

  8. 04004_使用JavaScript完成注册表单数据校验

    1.需求分析 (1)用户在进行注册的时候会输入一些内容,但是有些用户会输入一些不合法的内容,这样会导致服务器的压力过大,此时我们需要对用户输入的内容进行一个校验(前端校验和后台校验): (2)前端校验 ...

  9. Web安全解决方案

    什么是 .NET Framework 安全性? .NET Framework 提供了用户和代码安全模型,允许对用户和代码可以执行的操作进行限制.要对基于角色的安全性和代码访问安全性进行编程,可以从 S ...

  10. URI跟URL的区别

    关于URL和URI的区别,个人见解.    初学java,最近被一个概念搞得头晕脑胀,就是url和uri的概念和区别,网上查了一大通,发现各种回答眼花缭乱,有百科直接粘贴的,有胡说八道的,有故意绕来绕 ...