試題 算法提高 進制轉換
資源限制
時間限制:1.0s 內存限制:256.0MB
問題描述
編寫函數將十進制整數按8進制輸出,然後編寫main函數驗證該函數。例如輸入12,則輸出爲:12=014,輸入32,則輸出32=040。
輸入格式
輸入一個整數。
輸出格式
輸出這個整數的八進制形式。
樣例輸入
12
樣例輸出
014
數據規模和約定
輸入的整數n>=0.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
String result = Integer.toOctalString(i);
if (i == 0){
System.out.println(result);
}else {
System.out.println("0"+result);
}
}
}