소스 코드 제출
/*#include <stdio.h>
int stack[80000],top=-1;
void push(int h)
{
stack[++top]=h;
}
void pop()
{
if(top!=-1)
stack[top--]=0;
}
int main()
{
int n,h,i,j;
long long int sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&h);
int c=0;
for(j=top;j>=0;j--)
{
if(stack[j]<=h)
pop();
else
c++;
}
sum+=c;
push(h);
}
printf("%lld",sum);
return 0;
}
스택 쌓는거 vs 큐
스택 큐
구조 후입선출(LIFO) 선입선출(FIFO)
push top++; front++;
pop top--; back++;
포인터 top(맨위데이터위치) back 마지막데이터위치 (=top)
front 마지막나간데이터의위치
empty? top==-1 front==back
*/
#include <stdio.h>
int queue[200]={},front=-1,back=-1;
void push(int x)
{
queue[++back]=x;
}
void pop()
{
if(front!=back)
front++;
}
void frontf()
{
if(front==back)
printf("-1\n");
else
printf("%d\n",queue[front+1]);
}
void backf()
{
if(back==front)
printf("-1\n");
else
printf("%d\n",queue[back]);
}
void size()
{
printf("%d\n",back-front);
}
void empty()
{
if(front==back)
printf("true\n");
else
printf("false\n");
}
int main()
{
char stl[21]="";
int n,i,j;
scanf("%d",&n);
for(i=0;i<=n;i++)
{
gets(stl);
if(stl[1]=='u')
{
int k=0;
for(j=6;stl[j]!=32;j++)
k=k*10+stl[j]-48;
push(k);
}
else if(stl[1]=='o')
pop();
else if(stl[0]=='f')
frontf();
else if(stl[0]=='b')
backf();
else if(stl[0]=='s')
size();
else if(stl[0]=='e')
empty();
}
return 0;
}
좋아요