1. 程式人生 > >資料結構實驗之棧一:進位制轉換(java實現)

資料結構實驗之棧一:進位制轉換(java實現)

資料結構實驗之棧一:進位制轉換

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

輸入一個十進位制整數,將其轉換成對應的R(2<=R<=9)進位制數,並輸出。

Input

第一行輸入需要轉換的十進位制數;
第二行輸入R。

Output

輸出轉換所得的R進位制數。

Example Input

1279
8

Example Output

2377

Hint

Author

import java.util.Scanner;
import java.util.Stack;

public class Main {
	public static void main(String[] args) {
		
		Scanner cin = new Scanner(System.in);
		//System.out.println("請輸入需要轉換數字:");
		int num = cin.nextInt();
		//System.out.println("請輸入需要轉換的進位制(2~9):");
		int R = cin.nextInt();
		
		int ocNum = num;
		Stack stack = new Stack();

		while(num != 0) {	
			stack.push(num%R);
			num = num/R;
		}
		
		//System.out.print(ocNum + "轉換" + R + "進位制:");
		while(!stack.empty()) {
			System.out.print(stack.peek());
			stack.pop();
		}
	}
}