PAT 甲级 1001 A+B Format (20 分)(Java)

文章目录

PAT 甲级 1001 A+B Format (20 分)(Java)

题目

题目链接

解法

以下几种解法,我个人觉得解法一和解法二比较可行,解法四比较取巧;

解法一

这题最简便的方法,就是将数字转化为字符数组,然后找出当前数字在字符数组中的位数和字符数组长度的关系,也就是:(i+1) % 3 == ch.length % 3,然后将符号位和最后一位数字恰好有逗号的情况排除即可;

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();

        char[] ch = String.valueOf(a+b).toCharArray();

        for(int i=0; i<ch.length; ++i){
            System.out.print(ch[i]);
            if(ch[i] == '-'){
                continue;
            }
            if((i+1)%3 == ch.length%3 && i != ch.length-1){
                System.out.print(',');
            }
        }
    }
}

解法二

直接当做数字看待,因为是三个数字一组,可以对1000取余放进数组,然后除以1000,直至为0,最后倒序输出,注意:符号需要单独处理;

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = a + b;
        int[] res = new int[10];
        res[0] = 0;
        int index = 0;
        if(c == 0){
            System.out.println(0);
            return;
        }
        if(c < 0){
            System.out.print("-");
            c = -c;
        }
        while(c > 0){
            res[index++] = c % 1000;
            c /= 1000;
        }
        for(int i=index-1; i>=0; --i){
            String str;
            if(i != index-1){
                str = String.format("%03d", res[i]);
            }else{
                str = String.valueOf(res[i]);
            }
            if(i == index-1){
                System.out.print(str);
            }else{
                System.out.print("," + str);
            }
        }
    }
}

方法三

将其转换为字符串,然后通过String.subString()进行操作;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        boolean is = false;
        int result = a + b;
        if(result < 0){
            is = true;
            result = -result;
        }
        String s = String.valueOf(result);
        int len = s.length();
        int mod = len % 3;

        System.out.print(is ? "-" : "");
        if(len <= 3){
            System.out.print(s);
            return;
        }
        if(mod != 0 ){
            System.out.print(s.substring(0, mod)+",");
        }
        for(int i = mod; i < len; i+=3){
            if(i == mod){
                System.out.print(s.substring(i, i+3));
            }else{
                System.out.print("," + s.substring(i, i+3));
            }
        }
    }
}

方法四

这种方法比较取巧,因为数字的大小范围为正负1000000,所以就三种情况,直接列举;

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = a + b;
        if(c < 0){
            System.out.print("-");
            c = -c;
        }
        if(c >= 1000000){
            System.out.println(String.format("%1d", c/1000000) + "," + String.format("%03d", c/1000%1000) + "," + String.format("%03d", c%1000));
        }else if(c >= 1000){
            System.out.println(String.format("%1d", c/1000) + "," + String.format("%03d", c%1000));
        }else{
            System.out.println(c);
        }
    }
}
上一篇:【精讲】经典问题——0/1背包问题


下一篇:数据结构和算法-算法引入