【PAT】1022 D进制的A+B (20 分)

版权声明:版权所有,转载请注明出处。 https://blog.csdn.net/totalo/article/details/85631059

1022 D进制的A+B (20 分)

输入两个非负 10 进制整数 A 和 B (≤2​30​​−1),输出 A+B 的 D (1<D≤10)进制数。

输入格式:

输入在一行中依次给出 3 个整数 A、B 和 D。

输出格式:

输出 A+B 的 D 进制数。

输入样例:

123 456 8

输出样例:

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

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] strs = br.readLine().split(" ");
        int a = Integer.parseInt(strs[0]);
        int b = Integer.parseInt(strs[1]);
        int d = Integer.parseInt(strs[2]);
        int[] result = new int[105];
        int sum = a + b;
        int i = 0;
        while(sum >= 1){
            result[i++] = sum % d;
            sum = sum / d;
        }
        for (int j = i -1 ; j >= 0;j--){
            System.out.print(result[j]);
        }
        if (i == 0){
            System.out.print("0");
        }

    }

}

猜你喜欢

转载自blog.csdn.net/totalo/article/details/85631059