PAT 甲级 1058 A+B in Hogwarts [Java实现]

版权声明:转载请注明出处 https://blog.csdn.net/qq799028706/article/details/84497768

1. 题意

也是进制转换的题目
两个数字相加,然后第二项是17进制,第三项是29进制

2. 思路

水题。
注意最高位没有进位限制,一开始被这个坑了。

3. 代码

package adv1058;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author zmj
 * @create 2018/11/25
 */
public class Main {
    static final int length = 3;
    static int[] num = {Integer.MAX_VALUE, 17, 29};

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");
        br.close();
        
        String[] a = line[0].split("\\.");
        String[] b = line[1].split("\\.");
        int carry = 0;
        int[] res = new int[length];
        for (int i = length - 1; i >= 0; i--) {
            int sum = Integer.parseInt(a[i]) + Integer.parseInt(b[i]) + carry;
            res[i] = sum % num[i];
            carry = sum / num[i];
        }
        System.out.print(res[0] + "." + res[1] + "." + res[2]);
    }
}

猜你喜欢

转载自blog.csdn.net/qq799028706/article/details/84497768