1001 A+B Format (20point(s))(Java和C++)
文章目录
- 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 −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
一开始做题的时候搞错了一个点,导致测试点一半正确,一半错误:就是从前往后
每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++)相关推荐
- Pat甲级 1001 A+B Format
Pat甲级 1001 A+B Format 思路 代码 题目网址 https://pintia.cn/problem-sets/994805342720868352/problems/99480552 ...
- PAT甲级1001 A+B Format:[C++题解]字符串处理
文章目录 题目分析 题目链接 题目分析 数字转化成string 的函数to_string() 倒序遍历string,还保证原来的顺序需要 res = num[i] +res. 这里的加号不是数字运算, ...
- 【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 ...
- 题目1001 A+B Format
记录学习写博客的第一天(PAT题目) 题目1001 A+B Format Calculate a+b and output the sum in standard format – that is, ...
- 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 ...
- Eclipse配置阿里Code Format - 使用阿里Java Format 模板
1. 配置文件下载地址 https://github.com/alibaba/p3c/tree/master/p3c-formatter 下载下面两个文件: eclipse-codestyle.xml ...
- 1001 A+B Format (20分)——12行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Calculate a+b and output the sum in standard format – that is, th ...
- 1001. A+B Format (20)---------------PAT开始
今天是第一次用Java提交PAT的代码: 源代码不能直接提交,需要经过修改: 源代码: 1 package test;//提交时不能显示这句 2 3 import java.util.Scanner; ...
- 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 ...
最新文章
- POJ1269:Intersecting Lines(判断两条直线的关系)
- git add多个文件_Git之旅② - 子命令与设计哲学
- LeetCode 837. 新21点(动态规划)
- hadoop rpc客户端初始化和调用过程详解
- 成绩单表格html,【Web前端HTML5CSS3】15-表格
- notifier chain — 内核通知链【转】
- python定义变量类型列子_Python系列课程-变量类型
- 【渝粤教育】广东开放大学 集体谈判与集体合同 形成性考核 (30)
- Andrew Ng机器学习公开课笔记 -- Generative Learning algorithms
- BM3D算法半解,带python代码
- 2021-07-26 NLP词嵌入
- 服务器上系统使用排行,服务器操作系统使用排行榜
- 传华为公司又一名技术部员工乔向英猝死
- 不到一小时就可用Java做出坦克大战小游戏
- linux嵌入式reboot不生效,Embeded linux之reboot
- Linux手动安装JDK并配置多个版本JDK--JDK配置和Jenv的配置使用
- Wireshark入门与进阶---Capture Options各项的含义与设定
- STM32F105RBT6 uart调试
- 利用思维导图,快速整理小学语文复习重点,建议为孩子收藏!
- Zynq-Linux移植学习笔记
热门文章
- opencv 阈值分割_CVPR2019实例分割Mask Scoring RCNN
- jsp获取java数组长度_数组 – 如何在java jsp中获取数组列表大小?
- php中var_dump()函数
- UITextField中复制和粘贴为中文
- Swift之旅--数据类型
- NSFileHandle
- php swoole 项目实战,Laravel 中使用 swoole 项目实战开发案例一 (建立 swoole 和前端通信)...
- vue项目多页面入口配置
- Cisco 2811 IOS 升级实战
- Linux文件查找之findlocate