【C语言实现反转数组】(用栈实现)51nod - 训练营

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/81604761

题干:

输入一个长度为n(1 <= n <= 100000)数组,倒序输出他。

数组中的元素ai满足(1 <= ai <= 100000)。

Input

第一行一个整数n,表示数字长度
接下来n行,每行一个整数ai,表示数组的内容。

Output

输出第一行为数组长度n
接下来n行为倒序输出的结果。

Input示例

3
4
5
6

Output示例

3
6
5
4

解题报告:

      皮一下,用栈实现?

AC代码:

#include<bits/stdc++.h>

using namespace std;
int a[100000 + 5],n;
int main()
{
	int tmp;
	cin>>n;
	cout << n<<endl;
	stack<int > sk;
	while(n--) {
		cin>> tmp;
		sk.push(tmp);
	}
	while(sk.size()) {
		cout<<sk.top() << endl;
		sk.pop();
	}
	return 0 ;
}

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/81604761
今日推荐