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

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

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

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

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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 酒泉市| 略阳县| 罗山县| 如东县| 原平市| 邢台市| 蒲城县| 日土县| 酉阳| 道孚县| 五家渠市| 新竹市| 简阳市| 乐陵市| 新沂市| 开封市| 平顶山市| 科尔| 遵化市| 滦南县| 天等县| 东台市| 惠来县| 广州市| 隆林| 毕节市| 本溪市| 车险| 夹江县| 任丘市| 扶余县| 六盘水市| 凉山| 陵水| 昌都县| 且末县| 平塘县| 莱州市| 托里县| 岳西县| 蒲江县|