数据结构实验之栈一:进制转换(java实现)

数据结构实验之栈一:进制转换

Time Limit: 1000MS  Memory Limit: 65536KB
Submit  Statistic  Discuss

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();
		}
	}
} 


你可能感兴趣的:(java,栈,java,数据结构,栈)