PAT甲级——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)
Sample Input:
-1000000 9
Sample Output:
-999,991
第一种方法,注意题目说明的数字范围,及时处理越界即可。
为啥捏,因为 int 是32位的,最大2的31次方。题目给的数据容易越界
第二种方法,使用to_string
将A+B的值转化为字符串,按需要的格式进行修改。
string s = to_string(a + b);
to_string的用法推荐查阅官方文档,表述清晰
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
Convert numerical value to string
Returns a string with the representation of val.
第二种方法较为简单清晰。
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
string s = to_string(a + b);
int len = s.length();
for (int i = 0; i < len; i++) {
cout << s[i];
if (s[i] == '-')
continue;
if ((i + 1) % 3 == len % 3 && i != len - 1)
cout << ",";
}
return 0;
}
PAT甲级——1001 A+B Format (20分)的更多相关文章
- 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 ...
- PAT 甲级1001 A+B Format (20)(C++ -思路)
1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...
- PAT甲级 1001. A+B Format (20)
题目原文: Calculate a + b and output the sum in standard format -- that is, the digits must be separated ...
- PAT Advanced 1001 A+B Format (20 分)
Calculate a+b and output the sum in standard format -- that is, the digits must be separated into gr ...
- 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 ...
- PAT甲级:1152 Google Recruitment (20分)
PAT甲级:1152 Google Recruitment (20分) 题干 In July 2004, Google posted on a giant billboard along Highwa ...
- PAT甲 1001. A+B Format (20) 2016-09-09 22:47 25人阅读 评论(0) 收藏
1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate ...
- 【PAT】1001. A+B Format (20)
1001. A+B Format (20) Calculate a + b and output the sum in standard format -- that is, the digits m ...
- PAT甲级 1001 A+B Format
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 1001 A+B Format ( ...
随机推荐
- # vim ~/.vimrc vim配色
Ubuntu # vim ~/.vimrc # /hom/zzx 下 set nomodeline "(这个一定要写,目前有这个安全漏洞) set ...
- ntp时间同步服务器
[root@localhost ~]# ntpdate time.nist.gov 虚拟机时间不对 自动同步 sudo ntpdate asia.pool.ntp.org 要是同步后时间还是不 ...
- python保留小数位
前言 保留小数位是我们经常会碰到的问题,尤其是刷题过程中.那么在python中保留小数位的方法也非常多,但是笔者的原则就是什么简单用什么,因此这里介绍几种比较简单实用的保留小数位的方法: 方法一:fo ...
- JDK1.8 HashMap学习
1:源码分析 1.1:构造方法 public HashMap(int initialCapacity, float loadFactor) { ) throw new IllegalArgumentE ...
- 实验吧web-易-拐弯抹角(url伪静态)
<?php // code by SEC@USTC echo '<html><head><meta http-equiv="charset" c ...
- java中内存的划分
java中内存的划分 栈(stack):存放的都是方法中的局部变量.方法的运行一定要在栈当中运行. 局部变量:方法的参数,或者是方法{}内部的变量 作用域:一旦超出作用域,立刻从占内存当中消失. 堆( ...
- Vue-router(5)之 路由的before家族
beforeEach方法 import Vue from 'vue' import Router from 'vue-router' import Son1 from '@/view/New/son1 ...
- SJTU T4143 推箱子
问题描述 推箱子是一款经典的小游戏.游戏要求玩家将若干个箱子推到指定的位置,并以箱子移动次数最少作为目标. 现在,我们只需要考虑一个简化的版本--只有一个箱子.对于一张给定的地图,你需要判断是否可以将 ...
- delphi控制word 标题 字符和位置
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- 吴裕雄--天生自然深度学习TensorBoard可视化:改造后的mnist_train
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data INPUT_NODE = 784 ...