每日温度
#include<stdio.h>
#include<stdlib.h>
typedef int elemtype;
typedef struct {
elemtype a[20];
elemtype idx[20];
int top;
}Stack;
Stack* InitStack()
{
Stack *s=(Stack *)malloc(sizeof(Stack));
s->top=0;
return s;
}
int Empty(Stack *s)
{
if(s->top==0)
return 1;
else
return 0;
}
int push(Stack *s,elemtype x,int idx)
{
if(s->top==20)
return 0;
else{
s->a[s->top]=x;
s->idx[s->top]=idx;
s->top++;
return 1;
}
}
int pop(Stack *s,elemtype *x,int *idx)
{
if(Empty(s))
return 0;
else
{
--s->top;
*x=s->a[s->top];
*idx=s->idx[s->top];
return 1;
}
}
int GetTop(Stack *s)
{
if(Empty(s))
return 0;
else
{
return s->a[s->top-1];
}
}
int main()
{
int n;
printf("input the number of array\n");
scanf("%d",&n);
int a[n];
int i;
printf("please input the temperature\n");
for ( i=0;i<n;i++)
scanf("%d",&a[i]);
Stack *s=InitStack();
int result[n];
if(n==1)
{
printf("%d",0);
return 0;
}
if(n<1)
{
printf("the n is error\n");
return 0;
}
i=1;
int res_idx=0;
int x;
int idx;
push(s,a[0],0);
while(i<n)
{
while(!Empty(s))
{
if(a[i]>GetTop(s))
{
pop(s,&x,&idx);
result[idx]=i-idx;
}
else
{
push(s,a[i],i);
break;
}
}
if(Empty(s))
{
push(s,a[i],i);
}
i++;
}
while(!Empty(s))
{
pop(s,&x,&idx);
result[idx]=0;
}
for(idx=0;idx<n;idx++)
printf("%d ",result[idx]);
return 0;
}