【PAT】1022 D進位制的A+B (20 分)
阿新 • • 發佈:2019-01-06
1022 D進位制的A+B (20 分)
輸入兩個非負 10 進位制整數 A 和 B (≤230−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"); } } }