代码拉取完成,页面将自动刷新
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int N = 100010;
int a[N];
int stk[N] , hh = -1;
int n;
void baoli_solo(int a[],int n){
cout<<"-1 ";
for(int i = 1 ; i < n ; ++i){
int j = i - 1;
while( j >= 0){
if(a[j] < a[i]){
printf("%d ", a[j]);
break;
}
--j;
}
///////注意必须是严格小于0,才代表没找到比a[i]小的数
if(j < 0) printf("-1 ");
}
}
void res_solo(int a[] , int x){
// stk[++hh] = -1;
// cout<<stk[hh--]<<" ";
// cout<<-1<<endl;
vector<int> res;
for(int i = 0 ; i < n ;++i){
while(hh >= 0 && stk[hh] >= a[i]) {
cout<<"出栈元素: "<<stk[hh]<<endl;
--hh;
}
if(hh >= 0) {
res.push_back(stk[hh]);
cout<<a[i]<<"左边第一个小于的数:"<<stk[hh]<<endl;;
}
else {
res.push_back(-1);
// cout<<"-1 ";
}
// int j = i - 1;
// if(hh < 0 || a[i] > a[i - 1]) {////栈空则入栈
// if(hh < 0 || a[i] > stk[hh]) {
stk[++hh] = a[i];
cout<<"进栈元素: "<<stk[hh]<<endl;
// }
/////////如果 出现重复元素
// if(hh >= 0) cout<<stk[hh]<<" ";
// else cout<<"-1 ";
}
for(auto i : res){
cout<<i<<" ";
}
printf("\n");
}
void yxc(){
///将stk按照从hh = 0 表示没有元素
for(int i = 0 ; i < n ;++i){
int xi;
cin>>xi;
////出栈条件
while(hh && stk[hh] >= xi) --hh;
if(hh) cout<<stk[hh]<<" ";
else cout<<"-1 ";
stk[++hh] = xi;/////必入栈,栈顶不比xi小,就是空
}
}
// 5
// 3 4 2 7 5
// 6
// 3 3 4 2 7 5
int main(){
scanf("%d" , &n);
for(int i = 0 ; i < n ;++i) scanf("%d" , &a[i]);
// baoli_solo(a , n);
// cout<<endl;
res_solo(a , n);
printf("C 艹 mother fucker\n");
system("pause");
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。