国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

數據結構實驗之棧五:下一較大值(一)

2019-11-10 18:13:36
字體:
來源:轉載
供稿:網友

PRoblem Description

對于包含n(1<=n<=1000)個整數的序列,對于序列中的每一元素,在序列中查找其位置之后第一個大于它的值,如果找到,輸出所找到的值,否則,輸出-1。 Input 輸入有多組,第一行輸入t(1<=t<=10),表示輸入的組數;

以后是 t 組輸入:每組先輸入n,表示本組序列的元素個數,之后依次輸入本組的n個元素。 Output 輸出有多組,每組之間輸出一個空行(最后一組之后沒有);

每組輸出按照本序列元素的順序,依次逐行輸出當前元素及其查找結果,兩者之間以–>間隔。 Example Input

24 12 20 15 185 20 15 25 30 6

Example Output

12-->2020-->-115-->1818-->-120-->2515-->2525-->3030-->-16-->-1

Hint 本題的數據量小、限時要求低,可以不用棧來完成。 Author

#include <stdio.h>#include<math.h>#include <stack>#include <iostream>#include <algorithm>#include <bits/stdc++.h>using namespace std;struct node{ int data,id, next;};struct node ss[100100];int main(){ stack <struct node> p; int t; scanf("%d", &t); int tt=t; while(t--) { if(tt!=t+1)printf("/n"); while(!p.empty()) { p.pop(); } int n, a; scanf("%d", &n); for(a=1; a<=n; a++) { scanf("%d", &ss[a].data); ss[a].id=a; ss[a].next=-1; while(!p.empty()) { struct node k=p.top(); if(ss[a].data>k.data) { ss[k.id].next=ss[a].data; p.pop(); } else break; } p.push(ss[a]); } for(int a=1; a<=n; a++) { printf("%d-->%d/n", ss[a].data, ss[a].next); } } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汝城县| 临泽县| 合山市| 吴忠市| 界首市| 会昌县| 赫章县| 宿迁市| 正镶白旗| 栾川县| 景泰县| 互助| 通化市| 西丰县| 昭通市| 祁门县| 平阳县| 平顺县| 叶城县| 衢州市| 衡山县| 恩施市| 阿勒泰市| 芦山县| 静海县| 武强县| 灵山县| 静乐县| 博白县| 清徐县| 平遥县| 定襄县| 措美县| 繁峙县| 晋江市| 报价| 开封县| 奈曼旗| 自治县| 沂源县| 榆社县|