PAT:1001. A+B Format (20)
来源:互联网 发布:苹果铃声mac 编辑:程序博客网 时间:2024/06/07 01:32
时间限制
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 9Sample Output
-999,991
import java.io.BufferedInputStream;
import java.io.BufferedReader;import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.FileNotFoundException;
class ABFormat
{
public static void main(String[] args)
{
String lineTxt = null;
try{
String filePath="C:\\Users\\Administrator\\Desktop\\实验课\\PAT_Jdk\\input.txt";
File file = new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
lineTxt = bufferedReader.readLine();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] line=lineTxt.split(" ");
if(line.length>2){
System.out.println("the file is wrong");
System.exit(1);
}
long A=Integer.valueOf(line[0]);
System.out.printf("the A is %d",A);
long B=Integer.valueOf(line[1]);
System.out.printf("the B is %d",B);
long sum=A+B;
System.out.printf("the sum is %d",sum);
String sum_str=String.valueOf(sum);
System.out.println("the sum is "+sum_str);
int len=sum_str.length();
int i=len-1;
String tmp="";
System.out.printf("the tmp.len is %d",tmp.length());
while(i>=0)
{
tmp=sum_str.charAt(i)+tmp;
i--;
System.out.println("the tmp is"+tmp);
if(sum_str.charAt(i)!='-'){
if(tmp.length()%3==0){
System.out.printf("the tmp.len in if is %d",tmp.length());
tmp=","+tmp;
}
}
else{
break;
}
}
System.out.println("the answer is"+tmp);
}
}
0 0
- PAT A 1001. A+B Format (20)
- PAT-A-1001. A+B Format (20)
- PAT A 1001. A+B Format (20)
- PAT-A 1001. A+B Format (20)
- PAT-A 1001. A+B Format (20)
- Pat(A) 1001. A+B Format (20)
- PAT-A 1001. A+B Format (20)
- 【PAT】1001. A+B Format (20)
- PAT:1001. A+B Format (20)
- ( PAT )1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- [PAT]1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- 函数指针
- Java垃圾回收算法
- Swift 07 命令模式 Command Pattern
- 用Jquery动态append方式加入标签时 css样式丢失的解决方法
- POJ-2778 ac自动机+矩阵快速幂
- PAT:1001. A+B Format (20)
- hibernate注解方法使用总结
- Caffe 工程的一些编译错误以及解决方案(undefined reference to cv::imread)
- 求数值的的整数次方(模拟实现pow函数)
- codeforces-772C-Destroying Array
- FIFO深度计算分析
- Swift 08 备忘录模式 Memento Pattern
- byte(字节)根据长度转成KB(千字节)和MB(兆字节)
- 如何查询没有自增非空数字主键的表?