P1001 A+B Format
转跳点:
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 −106≤a,b≤106. 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- 最后一位,不需要判断
- 如果和小于0,那么需要跳过‘-’
- 注意数组大小,记得考虑进位和‘\0’以及‘-’
AC代码如下:
−106≤a,b≤106#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函数内置了对于整型的格式化
P1001 A+B Format的更多相关文章
- Spring resource bundle多语言,单引号format异常
Spring resource bundle多语言,单引号format异常 前言 十一假期被通知出现大bug,然后发现是多语言翻译问题.法语中有很多单引号,单引号在format的时候出现无法匹配问题. ...
- c# 字符串连接使用“+”和string.format格式化两种方式
参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...
- PAT甲级 1001. A+B Format (20)
题目原文: Calculate a + b and output the sum in standard format -- that is, the digits must be separated ...
- 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 ... 这个错误是因为有两个相 ...
- 【转】string.Format对C#字符串格式化
转自:http://blog.csdn.net/samsone/article/details/7556781 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) str ...
- VBA 格式化字符串 - Format大全
VBA 格式化字符串 VBA 的 Format 函数与工作表函数 TEXT 用法基本相同,但功能更加强大,许多格式只能用于VBA 的 Format 函数,而不能用于工作表函数 TEXT ,以下是本人归 ...
- [Erlang 0111] Erlang Abstract Format , Part 2
上回书,我们说到飞天玉虎蒋伯芳来到蜈蚣岭,不是,重来,上回咱们说到可以在Erlang Shell里面手工构造,加载并调用一个模块.在那个demo里面,我把多个Form单独生成出来,最后放在一起做 ...
- [Erlang 0110] Erlang Abstract Format , Part 1
Erlang Abstract Format并不难懂,只是枯燥一点罢了,如果把Abstract Format的文档翻译出来,其实就是Erlang教科书中语法入门的部分. Erlang Abstract ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
随机推荐
- 点击<a href="#">阻止自动跳转到顶部方法
最近开发web项目,遇到一个问题 ,就是在<a>标签加href="#",并增加onclick事件,页面会自动在点击该标签绑定的元素时,自动跳转到页面顶部,在网上寻求了一 ...
- 解决STM32工程出现:Undefined symbol TIM_ClearFlag (referred from hcsr04.o).错误。类型问题Undefined symbol TIM_xxx (referred from xxx.o).
出错原因: 工程FWLIB目录下没有添加stm32f10x_tim.c文件. 添加即可. 一般利用库开发,将ppp.c(ppp.c又调用了库stm32f10x_xx.h)写好之后的调用步骤: 举例使用 ...
- 数码管显示“0~F”的共阳共阴数码管编码表
嵌入式设备中数码管显示“0~F”的方式是:定义了一个数组,里面含有16个元素,分别代表0~F,这样可以方便以后的调用.共阳极数码管编码表:unsigned char table[]={0xc0,0xf ...
- Vue.nextTick DOM 更新循环结束之后执行延迟回调
在下次 DOM 更新循环结束之后执行延迟回调.在修改数据之后立即使用这个方法,获取更新后的 DOM. 简单来说,Vue 在修改数据后,视图不会立刻更新,而是等同一事件循环中的所有数据变化完成之后,再统 ...
- Django中url name
花了好长时间才明白这个name参数的含义.便写下来了备忘 当我们在url的时候,一般情况下都是使用很明确的url地址.如在网页里面使用<a href="/login"> ...
- [0CTF 2016] piapiapia
一道非常有意思的反序列化漏洞的题目 花费了我不少时间理解和记忆 这里简单记录其中精髓 首先打开是一个登陆页面 dirsearch扫描到了www.zip源码备份 update.php <?php ...
- Android基础 -- Activity之间传递数据(bitmap和map对象)
原文:http://blog.csdn.net/xueerfei008/article/details/23046341 做项目的时候需要用到在2个activity之间传递一些数据,之前做的都是些字符 ...
- 通过js检测浏览器支持的字体,从而显示支持的字体,让用户选择。
http://www.zhangxinxu.com/wordpress/2018/02/js-detect-suppot-font-family/ 本文根据张鑫旭文章. 字体函数: var dataF ...
- Mysql基本用法-01
#登录数据库 mysql -hlocalhost -uroot -p; #修改密码 mysqladmin -uroot -pold password new; #显示数据库 show database ...
- FormsAuthentication.HashPasswordForStoringInConfigFile方法再.net core中的替代代码
FormsAuthentication.HashPasswordForStoringInConfigFile()这个加密方法再.net core中不存在了,可以用下面的方式达到一样的加密效果 usin ...