文章目录

  • Java版
  • C++版

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

一开始做题的时候搞错了一个点,导致测试点一半正确,一半错误:就是从前往后每3个数出一个逗号。就像给的测试用例一样,但是这种方法只对字符串长度是3的整数倍的sum有效,要改成从后往前每3个数输出一个逗号
用一个特殊情况举例:
1000000+1000000=2000000

Java版

//20分
import java.io.*;
import java.math.BigInteger;public class Main {public static void main(String[] args) throws IOException {//定义输入BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));//输入两个整数String[] str = bf.readLine().split("\\s+");//将输入的字符串转化为BigInteger类型BigInteger a = new BigInteger(str[0]);BigInteger b = new BigInteger(str[1]);//计算两个整数加和并转化为String类型String sum = a.add(b) + "";//如果结果是负数,输出“-”,之后将字符串中的“-”去掉if (sum.charAt(0) == '-') {System.out.print("-");sum = sum.substring(1);}//遍历新字符串for (int i = 0; i < sum.length(); i++) {System.out.print(sum.charAt(i));//(i + 1) % 3 == sum.length() % 3是用来判断逗号要加的位置//(i != sum.length() - 1))用来保证输出的最后一位不输出逗号if ((i + 1) % 3 == sum.length() % 3 && (i != sum.length() - 1)) {System.out.print(",");}continue;}}
}

C++版

//参考柳神代码
//没想到思路和我的一样  												

1001 A+B Format (20point(s))(Java和C++)相关推荐

  1. Pat甲级 1001 A+B Format

    Pat甲级 1001 A+B Format 思路 代码 题目网址 https://pintia.cn/problem-sets/994805342720868352/problems/99480552 ...

  2. PAT甲级1001 A+B Format:[C++题解]字符串处理

    文章目录 题目分析 题目链接 题目分析 数字转化成string 的函数to_string() 倒序遍历string,还保证原来的顺序需要 res = num[i] +res. 这里的加号不是数字运算, ...

  3. 【PAT甲】1001 A+B Format (20分) 格式化输出

    problem 1001 A+B Format (20分) Calculate a+b and output the sum in standard format – that is, the dig ...

  4. 题目1001 A+B Format

    记录学习写博客的第一天(PAT题目) 题目1001 A+B Format Calculate a+b and output the sum in standard format – that is, ...

  5. PAT-PAT (Advanced Level) Practise 1001. A+B Format (20) 【二星级】

    题目链接:http://www.patest.cn/contests/pat-a-practise/1001 题面: 1001. A+B Format (20) Calculate a + b and ...

  6. Eclipse配置阿里Code Format - 使用阿里Java Format 模板

    1. 配置文件下载地址 https://github.com/alibaba/p3c/tree/master/p3c-formatter 下载下面两个文件: eclipse-codestyle.xml ...

  7. 1001 A+B Format (20分)——12行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Calculate a+b and output the sum in standard format – that is, th ...

  8. 1001. A+B Format (20)---------------PAT开始

    今天是第一次用Java提交PAT的代码: 源代码不能直接提交,需要经过修改: 源代码: 1 package test;//提交时不能显示这句 2 3 import java.util.Scanner; ...

  9. PAT (Advanced Level) Practise:1001. A+B Format

    [题目链接] Calculate a + b and output the sum in standard format -- that is, the digits must be separate ...

最新文章

  1. POJ1269:Intersecting Lines(判断两条直线的关系)
  2. git add多个文件_Git之旅② - 子命令与设计哲学
  3. LeetCode 837. 新21点(动态规划)
  4. hadoop rpc客户端初始化和调用过程详解
  5. 成绩单表格html,【Web前端HTML5CSS3】15-表格
  6. notifier chain — 内核通知链【转】
  7. python定义变量类型列子_Python系列课程-变量类型
  8. 【渝粤教育】广东开放大学 集体谈判与集体合同 形成性考核 (30)
  9. Andrew Ng机器学习公开课笔记 -- Generative Learning algorithms
  10. BM3D算法半解,带python代码
  11. 2021-07-26 NLP词嵌入
  12. 服务器上系统使用排行,服务器操作系统使用排行榜
  13. 传华为公司又一名技术部员工乔向英猝死
  14. 不到一小时就可用Java做出坦克大战小游戏
  15. linux嵌入式reboot不生效,Embeded linux之reboot
  16. Linux手动安装JDK并配置多个版本JDK--JDK配置和Jenv的配置使用
  17. Wireshark入门与进阶---Capture Options各项的含义与设定
  18. STM32F105RBT6 uart调试
  19. 利用思维导图,快速整理小学语文复习重点,建议为孩子收藏!
  20. Zynq-Linux移植学习笔记

热门文章

  1. opencv 阈值分割_CVPR2019实例分割Mask Scoring RCNN
  2. jsp获取java数组长度_数组 – 如何在java jsp中获取数组列表大小?
  3. php中var_dump()函数
  4. UITextField中复制和粘贴为中文
  5. Swift之旅--数据类型
  6. NSFileHandle
  7. php swoole 项目实战,Laravel 中使用 swoole 项目实战开发案例一 (建立 swoole 和前端通信)...
  8. vue项目多页面入口配置
  9. Cisco 2811 IOS 升级实战
  10. Linux文件查找之findlocate