PAT甲级练习1001. A+B Format (20)

来源:互联网 发布:无人机编程教材 编辑:程序博客网 时间:2024/06/05 14:47

1001. A+B Format (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

import java.util.Scanner;public class Main {public static void main(String[] args) {int a,b,c;Scanner s = new Scanner(System.in);a = s.nextInt();b = s.nextInt();c = a + b;System.out.printf("%,d", c);}}



Scanner类:

1. 导入import java.util.Scanner;

2. 实用API

delimiter() 
返回此 Scanner 当前正在用于匹配分隔符的 Pattern
hasNext() 
判断扫描器中当前扫描位置后是否还存在下一段
hasNextLine() 
如果在此扫描器的输入中存在另一行,则返回 true
next() 
        输入一个不含空格的字符串
nextInt()
输入一个整数
s.nextDouble()
输入一个double
s.nextByte()
输入一个字符
nextLine() 
此扫描器执行当前行,并返回跳过的输入信息


System.out.printf常用输出格式:

 System.out.printf("%+8.3f\n", 3.14); //"+"表示后面输出的数字会有正负号,正的+,负的- ;8.3f表示输出浮点数,宽度为8,小数点保持3位有效    System.out.printf("%+-8.3f\n", 3.14);//"-"表示靠左对齐   System.out.printf("%08.3f\n", 3.14);//"0"8位宽度中自动补0   System.out.printf("%(8.3f\n", -3.14);//"("如果是负数,自动加上( )   System.out.printf("%,f\n", 123456.78); //","金钱表示方法,每三位有一个逗号   System.out.printf("%x\n", 0x2a3b); //输出16进制数   System.out.printf("%#x\n", 0x2a3b);//输出带0x标识的16进制数   System.out.printf("老板:您名字%s,年龄:%3d岁,工资:%,-7.2f\n","ajioy",21,36000.00);   System.out.printf("老板:您名字%1$s,年龄:%2$#x岁\n","ajioy",38); //"n{1}quot;表示用第n个参数


0 0
原创粉丝点击