PRoblem Description 輸入一個(gè)十進(jìn)制數(shù)N,將它轉(zhuǎn)換成R進(jìn)制數(shù)輸出。
Input 輸入數(shù)據(jù)包含多個(gè)測(cè)試實(shí)例,每個(gè)測(cè)試實(shí)例包含兩個(gè)整數(shù)N(32位整數(shù))和R(2<=R<=16, R<>10)。
Output 為每個(gè)測(cè)試實(shí)例輸出轉(zhuǎn)換后的數(shù),每個(gè)輸出占一行。如果R大于10,則對(duì)應(yīng)的數(shù)字規(guī)則參考16進(jìn)制(比如,10用A表示,等等)。
Sample Input 7 2 23 12 -4 3
Sample Output 111 1B -11
import java.util.*;class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int N=sc.nextInt(); int R=sc.nextInt(); if(N<0){N=-N;System.out.print("-");} int x=N/R;int y=N%R; // 這個(gè)我想了很久才做出來(lái)當(dāng)時(shí)。 char chs[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; int []a=new int [50];int i=0; a[0]=N%R; while(x!=0){ i++; y=x%R; a[i]=y; x=x/R; } int b[]=new int[i+1]; for(int k=0;k<i+1;k++){ // System.out.print(a[k]+" a"); b[k]=a[i-k]; //System.out.print(b[k]+" "); } for(int j=0;j<b.length;j++){ System.out.print(chs[b[j]]); } System.out.println(); } }}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注