数据结构 考察栈的应用
int main()
{
int i,j,k,m,n;
while(scanf("%d",&n)==1)
{
cout<<"请输入进制数"<<endl;
scanf("%d",&m);
if(n==0)
{
cout<<0<<endl;
continue;
}
stack<int>s;
while(n)
{
int t=n%m;
s.push(t);
n/=m;
}
while(!s.empty())
{
printf("%d",s.top());
s.pop();
}
cout<<endl;
}
}
本文介绍了一个使用C++栈来实现十进制数到其他进制数转换的方法。通过不断取余并使用栈来反转输出结果,实现了从任意十进制整数到目标进制的转换。
1万+

被折叠的 条评论
为什么被折叠?



