欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-03-05 18:18:19
...

题目描述

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

代码

package com.hbut.pat;

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

public class Pat_1022 {
    public static void main(String[] args) throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	String[] arr = br.readLine().split("\\s+");
    	int A = Integer.parseInt(arr[0]);
    	int B = Integer.parseInt(arr[1]);
    	int D = Integer.parseInt(arr[2]);
    	int temp  = A + B;
    	if(D ==2){
    		System.out.println(Integer.toBinaryString(temp));
    	}else if(D == 8){
    		System.out.println(Integer.toOctalString(temp));
    	}else if(D == 16){
    		System.out.println(Integer.toHexString(temp));
    	}else{
    		System.out.println(Integer.toString(temp, D));
    	}
    }
}