十進制轉八進制

題目

編寫函數,其功能爲把一個十進制數轉換爲其對應的八進制數。程序讀入一個十進制數,調用該函數實現數制轉換後,輸出對應的八進制數。

樣例輸入
9274
樣例輸出
22072
樣例輸入
18
樣例輸出
22

源碼

import java.util.*;
public class Main {   
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[]jz=new int[1000000];
        if(n==0)
            System.out.print(n);
        else
        {
            int i=0;
            while(n!=0){
                jz[i]=n%8;
                n/=8;
                i++;
            }
            for(i=i-1;i>=0;i--)
                System.out.print(jz[i]);
        }
        System.out.println();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章