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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值