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

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

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

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

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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中牟县| 桐梓县| 晴隆县| 东阳市| 江达县| 青州市| 广德县| 同心县| 东乡| 平武县| 梁平县| 大庆市| 南汇区| 井冈山市| 陈巴尔虎旗| 和林格尔县| 宜城市| 寿宁县| 长乐市| 洛隆县| 定州市| 靖安县| 兴业县| 台东县| 宿州市| 徐闻县| 红河县| 尼勒克县| 大埔区| 金乡县| 昌平区| 绥中县| 怀集县| 沾化县| 社会| 临邑县| 延津县| 于田县| 泸西县| 南丹县| 凭祥市|