数据结构实验之栈一:进制转换
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
01 | #include<iostream> |
02 | #include<stack> |
03 | using namespace std; |
04 | int main() |
05 | { |
06 | stack< int >p; //声明了1个存储int型元素的栈,栈名是p. |
07 | int n; //进制数 |
08 | int m; //待转换的数 |
09 | cin >>m>>n; |
10 | if (m==0) //请单独判断,否则empty()会出现error,不知道为什么 |
11 | cout<<0<<endl; |
12 | while (m>0) //条件注意! |
13 | { |
14 | p.push(m%n); //存余数 |
15 | m/=n; //利用n进制的定义,短除法,最后从下往上输出 |
16 | } |
17 | while (!p.empty()) |
18 | { |
19 | cout<<p.top(); //按位输出 |
20 | p.pop(); //消除 |
21 | } |
22 | cout<<endl; //注意换行 |
23 | return 0; |
24 | } |