数据结构实验之栈与队列五:下一较大值(一)
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description
对于包含n(1<=n<=1000)个整数的序列,对于序列中的每一元素,在序列中查找其位置之后第一个大于它的值,如果找到,输出所找到的值,否则,输出-1。
Input
输入有多组,第一行输入t(1<=t<=10),表示输入的组数;
以后是 t 组输入:每组先输入n,表示本组序列的元素个数,之后依次输入本组的n个元素。
Output
输出有多组,每组之间输出一个空行(最后一组之后没有);
每组输出按照本序列元素的顺序,依次逐行输出当前元素及其查找结果,两者之间以–>间隔。
Sample Input
2
4 12 20 15 18
5 20 15 25 30 6
Sample Output
12–>20
20–>-1
15–>18
18–>-1
20–>25
15–>25
25–>30
30–>-1
6–>-1
Hint
本题的数据量小、限时要求低,可以不用栈来完成。
#include <iostream>
#include <string>
#include<stdio.h>
#include<stdlib.h>
#define max 1000
using namespace std;
typedef int Elemtype;
typedef struct
{
Elemtype *top;
Elemtype *base;
}Sqstack;
void initstack(Sqstack &S)
{
S.base = (Elemtype *)malloc(max *sizeof(Elemtype));
S.top = S.base;
}
void push(Sqstack &S, Elemtype e)
{
*S.top++ = e;
}
Elemtype gettop(Sqstack S)
{
Elemtype e;
e = *(S.top-1);
return e;
}
Elemtype pop(Sqstack &S)
{
Elemtype e;
e = *--S.top;
return e;
}
int judge(char a, char b)
{
if((a=='('&&b==')')||(a=='{'&&b=='}')||(a=='['&&b==']'))
return 1;
else
return 0;
}
int emptystack(Sqstack &S)
{
if(S.top==S.base)
return 1;
else
return 0;
}
int main()
{
int t, n, i, j, k;
int s[1011];
cin>>t;
for(k=0; k<t; k++)
{
cin>>n;
for(i=0; i<n; i++)
cin>>s[i];
for(i=0; i<n; i++)
{
cout<<s[i]<<"-->";
for(j=i+1; j<n; j++)
{
if(s[j]>s[i])
{
cout<<s[j]<<endl;
break;
}
}
if(j==n)
cout<<"-1"<<endl;
}
if(k!=t-1)
cout<<endl;
}
return 0;
}
/***************************************************
User name: jk160532姜兴友
Result: Accepted
Take time: 0ms
Take Memory: 228KB
Submit time: 2017-10-16 13:36:19
****************************************************/