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

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

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

2019-11-10 18:33: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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彰化市| 乐至县| 新丰县| 灌阳县| 河津市| 镇巴县| 泰安市| 吐鲁番市| 曲松县| 车险| 融水| 灵武市| 水城县| 昂仁县| 万源市| 泰顺县| 黔西县| 靖宇县| 纳雍县| 尚义县| 高密市| 尼玛县| 乌恰县| 东乡县| 全南县| 上蔡县| 宁津县| 鲁山县| 黄石市| 湖南省| 乌兰察布市| 阿城市| 甘孜县| 英德市| 大理市| 洛隆县| 镇巴县| 和田县| 沾益县| 乳山市| 浏阳市|