转跳点:

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 两个数相加,且 A B两个数 限制为 −10​6​​≤a ,b≤10​,然后按照国际标准给数字填上,我偷了个懒, A, B整型输入, 和存储在字符串里,然后%c一个个输出
这里需要注意几个输出点:
  1.   最后一位,不需要判断
  2.   如果和小于0,那么需要跳过‘-’
  3.   注意数组大小,记得考虑进位和‘\0’以及‘-’

AC代码如下:

−10​6​​≤a,b≤10​6#include <stdio.h>
#include <stdlib.h>
#include <string.h> int main(void)
{
char str[9];
int A, B; scanf("%d %d", &A, &B);
sprintf(str, "%d", (A + B));
int len = strlen(str); for (int i = 0; i < len; i++)
{
printf("%c", str[i]);
if (len - 1 == i || '-' == str[i])
{
continue;
}
if (0 == (len - 1 - i) % 3)
{
printf(",");
}
} return 0;
}

C++版本:

  我找到了一个库<sstream>这个库实现了sprintf的功能比sprintf更加安全具体使用方法,看这篇博客U•ェ•*U

#include <iostream>
#include <sstream> using namespace std; int main()
{
int A, B;
cin >> A >> B; stringstream sStream;
sStream << A + B; //将A+B的和放入stringstream的缓存区域
string s(sStream.str()); //将sStream里的值按照某种数据格式写入s里
//这个过程类似于sprintf() int cnt = 0;
for (string::iterator it = s.end() - 1; it != s.begin(); --it)
{//string内置的迭代器
(++cnt) %= 3;
if (cnt == 0 && !((it - s.begin() == 1) && s.front() == '-'))
{
s.insert(it, ',');
}
} cout << s << endl; return 0;
}

更新:2020-02-18 14:12:18

  最近在学Python,思考了一波,嗯,python大法好啊,不过我还是热衷于C/C++

a, b = map(int, input().split())   #format函数
print(format(a+b, ','))        #format函数内置了对于整型的格式化

 PAT不易,诸君共勉

P1001 A+B Format的更多相关文章

  1. Spring resource bundle多语言,单引号format异常

    Spring resource bundle多语言,单引号format异常 前言 十一假期被通知出现大bug,然后发现是多语言翻译问题.法语中有很多单引号,单引号在format的时候出现无法匹配问题. ...

  2. c# 字符串连接使用“+”和string.format格式化两种方式

    参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...

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

    题目原文: Calculate a + b and output the sum in standard format -- that is, the digits must be separated ...

  4. Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...

    Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...

  5. 【转】string.Format对C#字符串格式化

    转自:http://blog.csdn.net/samsone/article/details/7556781 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) str ...

  6. VBA 格式化字符串 - Format大全

    VBA 格式化字符串 VBA 的 Format 函数与工作表函数 TEXT 用法基本相同,但功能更加强大,许多格式只能用于VBA 的 Format 函数,而不能用于工作表函数 TEXT ,以下是本人归 ...

  7. [Erlang 0111] Erlang Abstract Format , Part 2

       上回书,我们说到飞天玉虎蒋伯芳来到蜈蚣岭,不是,重来,上回咱们说到可以在Erlang Shell里面手工构造,加载并调用一个模块.在那个demo里面,我把多个Form单独生成出来,最后放在一起做 ...

  8. [Erlang 0110] Erlang Abstract Format , Part 1

    Erlang Abstract Format并不难懂,只是枯燥一点罢了,如果把Abstract Format的文档翻译出来,其实就是Erlang教科书中语法入门的部分. Erlang Abstract ...

  9. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

随机推荐

  1. 关于emoji表情,支持在app端发送web端显示,web端发送给app端显示,web与wap端互相显示。

    要用到emoji.js和emoji.jquery.js两个插件配合实现三端互通. 1.app端发送的emoji表情----到服务器---服务器存储的是‘问号’,无法显示如图所示: 后台的同学也试验了网 ...

  2. 《一篇文章读懂HTTPS及其背后的加密原理》阅读笔记

    HTTPS(Hypertext Transfer Protocol Secure,超文本传输安全协议),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.这篇文章深入介绍了它的原理. 当我们适用 ...

  3. 一个HTTP数据包的奇幻之旅

    我是一个HTTP数据包,不知谁创建了我,把我丢到这个房间. 突然,来了一个大汉,我吓得缩到角落. “该启程了,站起来”. “去哪里啊?” 我弱弱的问. “还能去哪里,你是一个数据包,当然要出远门,完成 ...

  4. 炼金术(2): 为什么要用issue管理软件

    在项目开发中,存在的无数的任务分解,问题管理,流程跟踪.因为直接说话或者直接在IM里喊话是很容易的,所以在一个还没有习惯使用issue管理软件的团队中,直接说话或者直接在IM里AT,就在某些时候变成了 ...

  5. Mobility Express部署外部镜像服务器

    1.当我们部署完ME的时候,发现有一些AP虽然显示已经加入了WLC(ME),但是它其实并没有正常的工作,显示不可用: (Cisco Controller) >show ap su Number ...

  6. 的aspnet_client文件夹

    在早期,有一些asp.net组件是默认要调用(从客户端调用)服务器根(域名)下这个子目录里边的文件的. 不过如果你使用高版本的asp.net,那么全都从你的网站里调用了,因为asp.net有了更好地直 ...

  7. 第七届蓝桥杯javaB组真题解析-剪邮票(第七题)

    题目 /* 剪邮票 如[图1.jpg], 有12张连在一起的12生肖的邮票. 现在你要从中剪下5张来,要求必须是连着的. (仅仅连接一个角不算相连) 比如,[图2.jpg],[图3.jpg]中,粉红色 ...

  8. Pycharm 在 import requests 时报错

    requests的安装我尝试了两种方法: 第一种:下载安装 https://github.com/requests/requests 然后解压到:(python的安装路径,这里使用的是默认路径) Ap ...

  9. Python3.5学习之旅——day2

    本节内容: 1.模块初识 2..pyc是什么? 3.Python的数据类型 4.三元运算 5.进制 6.byte类型 7.数据运算 8.列表 9.元组 10.课后练习 一.模块初识 由day1的学习我 ...

  10. 面试官:说说Spring中的事务传播行为

    前言 在开发中,相信大家都使用过Spring的事务管理功能.那么,你是否有了解过,Spring的事务传播行为呢? Spring中,有7种类型的事务传播行为.事务传播行为是Spring框架提供的一种事务 ...