PAT 1001.A+B Format
2015-07-14 21:42
477 查看
原题地址: http://www.patest.cn/contests/pat-a-practise/1001
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).
Input
Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.
Output
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
本题
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).
Input
Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.
Output
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
本题
package com.dye.pat.t1001; import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner in = new Scanner(System.in); String[] input = in.nextLine().split(" "); in.close(); int a = Integer.parseInt(input[0].trim()); int b = Integer.parseInt(input[1].trim()); String result = "" + (a + b); String flag = ""; if (result.charAt(0) == '-') { flag = "-"; result = result.substring(1); } int count = result.length(); count = (3 - count % 3) % 3; for (int i = 0; i < result.length(); i++) { if (count !=0 &&count % 3 == 0) flag = flag + ","; flag += result.substring(i, i+1); count++; } System.out.println(flag); } }
相关文章推荐
- 盘古分词
- 去掉两个最高分、去掉两个最低分,求平均分
- 集合
- Linux中epoll用法小结(转载)
- java实现各种排序算法
- IndentationError: unexpected indent python
- Java字符串的分割方法
- 循环select查询结果集
- [Python]Django部署问题
- java高新技术下《九》
- 欢迎使用CSDN-markdown编辑器
- 共勉
- 第8章 基于重新排序的排名方法 阅读
- 投票选班长
- 234-e-Palindrome Linked List
- windows下安装redis
- Windows OS 下我做为开发人员常用的一些软件
- MySQL导入.sql文件及常用命令
- myeclipse创建user library
- WebService详解