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分)的更多相关文章

  1. 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 ...

  2. 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 ...

  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. 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 ...

  5. 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 ...

  6. PAT甲级:1152 Google Recruitment (20分)

    PAT甲级:1152 Google Recruitment (20分) 题干 In July 2004, Google posted on a giant billboard along Highwa ...

  7. 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 ...

  8. 【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 ...

  9. PAT甲级 1001 A+B Format

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 1001 A+B Format ( ...

随机推荐

  1. ssi引用路径规则

    1.file:引用的是相对路径,相对当前文件的,特别注意使用file,被引用文件只能与当前文件同级.或下级.不能在其上级(不支持../) <!--#include file="head ...

  2. 深入X64架构(翻译)

    | 本人只是原创翻译,而且翻译也不一定好,纯当锻炼.内容如果英文好的同学,建议直接去看英文原版,比较爽. NBAOL系列2代产品是 windows平台64位的应用程序,在技术测试过程中,遇到一些cra ...

  3. mysql数据库索引优化

    参考 :http://www.cnblogs.com/yangmei123/archive/2016/04/10/5375723.html MySQL数据库的优化:    数据库优化的目的:     ...

  4. 实验吧web-中-简单的sql注入

    页面显示:到底过滤了什么东西? 所以我们先试试到底是过滤了什么 1 显示正常 1' 不正常 (直接输入的关键字均会被过滤) 1 union select 显示:1 select 1 union sel ...

  5. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring Bean的生命周期

    Spring 容器可以管理 singleton 作用域 Bean 的生命周期,在此作用域下,Spring 能够精确地知道该 Bean 何时被创建,何时初始化完成,以及何时被销毁. 而对于 protot ...

  6. SQL基础之JDBC、连接池

    JDBC JDBC四个核心对象 这几个类都是在java.sql包中 DriverManager(类): 数据库驱动管理类.这个类的作用:1)注册驱动; 2)创建java代码和数据库之间的连接,即获取C ...

  7. ES6 之 字符串扩展

    字符串所有的方法,不会修改字符串本身(字符串是不可变的),操作完成会 返回一个新的字符串. 将一个值转化为字符串,.toString()方法,但是null和undefined值没有这个方法, 1.字符 ...

  8. UML-用例关联

    1.用例关联:就是各个用例之间的关系,分3种关系分别是:包含关系.扩展关系.泛化关系. 2.包含关系 1).示例 2).使用场景 A.用例在其他用例中重复使用 B.用例非常复杂冗长,将其分解为子单元便 ...

  9. UML-重构

    1.重构是什么? 重构是重写或重新构建已有代码的结构化和规律性方法,但不会改变已有代码的外在行为,而是采用一系列少量转换的步骤,并且每一步都结合了重新执行的测试.重构并不是全部推翻原有代码结构. 2. ...

  10. 并发与高并发(七)-线程安全性-原子性-atomic

    一.线程安全性定义 定义:当多个线程访问某个类时,不管运行时环境采用何种调度方式或者这些线程将如何交替执行,并且在主调代码中不需要任何额外的同步或协同,这个类都能表现出正确的行为,那么就称这个类是线程 ...