第一次用stack,挺好用的,先進後出的特性很實用
c++ stl棧stack的頭檔案為:
#include <stack>
c++ stl棧stack的成員函數介紹
操作 比較和配置設定堆棧
empty() 堆棧為空則傳回真
pop() 移除棧頂元素
push() 在棧頂增加元素
size() 傳回棧中元素數目
top() 傳回棧頂元素
下面是杭電2031的代碼!
#include<iostream>
#include<stack>
using namespace std;
int main()//利用棧的先進後出 的特性
{
int n;
int r;
stack<char>s;
while(cin>>n>>r)
{
bool flag=true;
if(n<0)
{
flag=false;
n=-n;
}
while(n>0)
{
int temp=n%r;
n/=r;
if(temp>9)
{
temp=temp-10+'A';
}
else
{
temp=temp+'0';
}
s.push(temp);//将temp存到棧s中
}
if(!flag)
{
s.push('-');
}
while(!s.empty())
{
cout<<s.top();//輸出棧頂端的元素
s.pop();//删除棧頂端的元素
}
cout<<endl;
}
return 0;
}