【JAVA】1001 A+B Format (20分) PAT甲级 PAT (Advanced Level) Practice
目录
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 Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where − ≤a,b≤ . 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
思路一:不按他们的位数分情况处理
我将两种代码合并在了一起,大家进代码看注释
代码一:用可变数组ArrayList+字符串截取方法subString
代码二:栈Stack+字符串截取方法subString
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
public class APlusB1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
sc.close();
System.out.print(add(a, b));
}
public static String add(int a, int b) {
String sum = a + b + "";
if (a + b > 0) {
if (sum.length() <= 3) {
return sum;
} else {
return addFormat(sum);
}
} else if (a + b < 0) {
sum = sum.substring(1);//取负号之后的数
if (sum.length() <= 3) {
return "-" + sum;
} else {
return "-" + addFormat(sum);
}
} else {
return "0";
}
}
public static String addFormat(String sum) {
int max = sum.length();
String str;
String str1;
StringBuilder str2 = new StringBuilder();
/*Stack<String> stack = new Stack<String>();*/
ArrayList<String> arrayList = new ArrayList<String>();
do {//因为 sum.length() > 3,才会到该方法,所以要先进行一次运算,而不是先判断
str = sum.substring(max - 3, max);
str1 = sum.substring(0, max - 3);
/*stack.push("," + str);*/
arrayList.add("," + str);
max = max - 3;
} while (max > 3);
/*while (!stack.empty()) {
str2.append(stack.pop());
}*/
for (int i = arrayList.size() - 1; i >= 0; i--) {
str2.append(arrayList.get(i));
}
return str1 + str2;
}
}
思路二:因为a和b的范围较小,所以可以按他们的位数分情况处理
代码一:字符串截取方法substring
import java.util.Scanner;
public class APlusB3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
sc.close();
System.out.print(addFormat(a, b));
}
public static String addFormat(int a, int b) {
String sum = a + b + "";
String str = "";
if (a + b < 0) { //处理负数
str = "-"; //尽量少用“+”, str += "-",因为会创建SringBuilder对象,增加运行时间
sum = sum.substring(1); //sum = ""+(0-(a+b))
}
int length = sum.length();
if (length >= 7) {
str += sum.substring(0, length - 6) + "," + sum.substring(length - 6, length - 3) + "," + sum.substring(length - 3, length);
} else if (length >= 4) {
str += sum.substring(0, length - 3) + "," + sum.substring(length - 3, length);
} else {
str += sum;
}
return str;
}
}
代码二:StringBuilder的insert方法
import java.util.Scanner;
public class APlusB4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
sc.close();
System.out.print(addFormat(a, b));
}
public static StringBuilder addFormat(int a, int b) {
StringBuilder sum = new StringBuilder(a + b + "");
String negative = "";
if (a + b < 0) { //处理负数
negative = "-"; //尽量少用“+”, negative += "-",因为会创建SringBuilder对象,增加运行时间
sum = new StringBuilder("" + (0 - (a + b))); //去掉负号
}
int length = sum.length();
if (length >= 7) {//其实length最大为7,因为a和b最大为10的6次方
sum.insert(length - 3, ",");
sum.insert(length - 6, ",");
} else if (length >= 4) {
sum.insert(length - 3, ",");
}
return sum.insert(0, negative);
}
}
代码三:格式化输出 printf
%03d
表示显示为三位十进进制数
d表示十进制数,3表示显示长度,
0表示不足三位的前补0
import java.util.Scanner;
public class APlusB2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
scanner.close();
int sum = a + b;
if (sum < 0) {
System.out.print("-");
sum = 0 - sum;
}
if (sum >= 1000000) {
System.out.printf("%d,%03d,%03d", sum / 1000000, sum % 1000000 / 1000, sum % 1000);
//%03d表示显示为三位十进进制数 d表示十进制数,3表示显示长度,0表示不足三位的前补0
} else if (sum >= 1000) {
System.out.printf("%d,%03d", sum / 1000, sum % 1000);
} else {
System.out.println(sum);
}
}
}
大家好,我是[爱做梦的子浩](https://blog.csdn.net/weixin_43124279),我是东北大学大数据实验班大三的小菜鸡,非常向往优秀,羡慕优秀的人,已拿两个暑假offer,欢迎大家找我进行交流😂😂😂
这是我的博客地址:[子浩的博客https://blog.csdn.net/weixin_43124279]
——
版权声明:本文为CSDN博主「爱做梦的子浩」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。