用栈来解决c++问题,例1:

/*题目简介:把看到的一串数字(长度不一定,以0结束,最多不超过100个,

数字不超过2^32-1)
记住了然后反着念出来(表示结束的数字0就不要念出来了)。
编程解决这个问题。*/

#include <bits/stdc++.h> //万能头文件,不说什么了,自己上网搜

using namespace std;

int main()

{

stack<int> s;//定义一个栈

while (1)

{

int a; scanf("%d",&a);

扫描二维码关注公众号,回复: 107320 查看本文章

if (!a) break;//判断是否结束

s.push(a);//将a压入栈

}

while (!s.empty())

{

cout<<s.top();//输出栈顶元素

printf(" "); s.pop();//栈顶第一个元素出战

}

cout<<"\n";//换行别理它=-= return 0;

}

猜你喜欢

转载自www.cnblogs.com/Michael666/p/8987806.html