ZZULIOJ 1144: Multiple bases, Java

ZZULIOJ 1144: Multiple bases, Java

Question description

Input a decimal integer n and convert it into base 2, 3, 7, or 8 for output.
The program is required to define a dToK() function, whose function is to convert the decimal number into a base k integer. The rest of the functions are implemented in the main() function.
void dToK(int n, int k, char str[])
{ //Convert n to base k number and store it in str }

enter

Enter a positive integer n in the range of int

output

The output is 4 lines, which are the 2, 3, 7 and 8 base numbers corresponding to n.

Sample inputCopy
13
Sample outputCopy
1101
111
16
15
import java.io.*;

public class Main {
    
    
    public static void main(String[] args) throws IOException {
    
    
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int n = Integer.parseInt(bf.readLine());
        String binary = Integer.toBinaryString(n);
        String ternary = Integer.toString(n, 3);
        String septenary = Integer.toString(n, 7);
        String octal = Integer.toOctalString(n);
        bw.write(binary + "\n" + ternary + "\n" + septenary + "\n" + octal);
        bw.close();
    }
}

Guess you like

Origin blog.csdn.net/qq_52792570/article/details/132545562