我是靠谱客的博主 文静小熊猫,最近开发中收集的这篇文章主要介绍【JAVA】1001 A+B Format (20分) PAT甲级 PAT (Advanced Level) Practice1001 A+B Format (20分)思路一:不按他们的位数分情况处理思路二:因为a和b的范围较小,所以可以按他们的位数分情况处理,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目录

  • 1001 A+B Format (20分)
  • 思路一:不按他们的位数分情况处理
    • 代码一:用可变数组ArrayList+字符串截取方法subString
    • 代码二:栈Stack+字符串截取方法subString
  • 思路二:因为a和b的范围较小,所以可以按他们的位数分情况处理
    • 代码一:字符串截取方法substring
    • 代码二:StringBuilder的insert方法
    • 代码三:格式化输出 printf

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 − 1 0 6 10^6 106​​ ≤a,b≤ 1 0 6 10^6 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

思路一:不按他们的位数分情况处理

我将两种代码合并在了一起,大家进代码看注释

代码一:用可变数组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

  1. %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);
}
}
}

最后

以上就是文静小熊猫为你收集整理的【JAVA】1001 A+B Format (20分) PAT甲级 PAT (Advanced Level) Practice1001 A+B Format (20分)思路一:不按他们的位数分情况处理思路二:因为a和b的范围较小,所以可以按他们的位数分情况处理的全部内容,希望文章能够帮你解决【JAVA】1001 A+B Format (20分) PAT甲级 PAT (Advanced Level) Practice1001 A+B Format (20分)思路一:不按他们的位数分情况处理思路二:因为a和b的范围较小,所以可以按他们的位数分情况处理所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部