2239: 十进制与八进制的转换(栈和队列)
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2 Solved: 2
[Submit][Status][Web Board]
Description
对于输入的任意一个非负十进制整数,利用栈打印输出与其等值的八进制数。
Input
111
Output
157
Sample Input
148
Sample Output
224
HINT
Source
Code:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2239: 十进制与八进制的转换(栈和队列) 2 所用操作函数: 3 empty() 堆栈为空则返回真 4 pop() 移除栈顶元素 5 push() 在栈顶增加元素 6 top() 返回栈顶元素 7 8 思路: 9 用依次相除求模法计算 10进制->8进制 10 将求出的模依次压入栈中 11 最后依次输出就是所求的8进制数 12 */ 13 14 #include <iostream> 15 #include <stack> 16 using namespace std; 17 18 int main() 19 { 20 int num; 21 while(cin>>num){ 22 stack <int> s; 23 while(num/8>0){ 24 s.push(num%8); //倒序压倒栈里 25 num/=8; 26 } 27 s.push(num); 28 while(!s.empty()){ 29 cout<<s.top(); 30 s.pop(); 31 } 32 cout<<endl; 33 } 34 return 0; 35 } 36 37 /************************************************************** 38 Problem: 2239 39 User: freecode 40 Language: C++ 41 Result: Accepted 42 Time:0 ms 43 Memory:1268 kb 44 ****************************************************************/
Freecode : www.cnblogs.com/yym2013