PAT乙级:1022 D进制的A+B(JAVA)

import java.util.*;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner reader = new Scanner(System.in);
		int a = reader.nextInt();
		int b = reader.nextInt();
		int d = reader.nextInt();
		int sum = a+b;
        if(sum == 0) {
			System.out.println("0");
		}
		String str = nary(sum,d);
		for(int i=str.length()-1;i>=0;i--) {
			System.out.print(str.charAt(i));
		}
//		System.out.println(nary(sum,d));
	}
	public static String nary(int sum, int d) {
		String str = "";
		while(sum != 0) {
			str += sum%d;
			sum/=d;
		}
		return str;
	}
}

测试点3是当a=0,b=0时的情况,这时候直接输出0即可

你可能感兴趣的:(PAT乙级)