STL的应用https://ac.nowcoder.com/acm/problem/14893
利用STL里面的栈进行操作,这些数1~n的一个排列,要让他降序输出,可以从令j等于n到1依次判断,首先一个循环让元素入栈,同时判断栈顶元素是否和j相等,如果相等则输出并删除栈顶元素,让j--,要是不相等则让下一个元素入栈,待所有元素全部入栈,栈不为空则输出栈内的元素即可,最后就会是降序的序列,或者是按字典序最大的排列。
#include <bits/stdc++.h> #include <algorithm> #define mod 1000000007 #define LL long long using namespace std; int a[1000005]; int main() { stack<int>s; int n,i,j,temp,t,m; cin >> n; m = n; for(i = 1; i <= n; i++) cin >> a[i]; j = 1; for(i = 1; i <= n; i++){ if(s.empty()){ s.push(a[i]); temp = s.top(); if(temp == m){ cout << temp << " "; m--; s.pop(); continue; } continue; } temp = s.top(); if(temp == m){ cout << temp << " "; m--; s.pop(); s.push(a[i]); continue; } s.push(a[i]); } while(!s.empty()){ cout << s.top() << " "; s.pop(); } return 0; }