對于包含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 6Example Output
12-->2020-->-115-->1818-->-120-->2515-->2525-->3030-->-16-->-1Hint 本題的數據量小、限時要求低,可以不用棧來完成。 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;}新聞熱點
疑難解答