The sword refers to the 21st question on offer, before in cracking the coding interview has done, the thought reference here, this time wrote the test function, in nine degrees OJ test passes.
Topic Description:
To define the data structure of the stack, implement a min function that gets the minimum stack element in the type.
Input:
The input may contain more than one test sample, and the input ends with EOF.
For each test case, the first behavior of the input is an integer n (1<=n<=1000000), and N represents the number of steps to be entered for the operation.
Next there are n lines, and each line begins with a letter ci.
Ci= ' s ', then there is a number k, which means to press K into the stack.
Ci= ' O ' when pop-up stack top element.
Output:
Corresponds to each action in each test case,
If the stack is not empty, output the corresponding minimum element of the stack. Otherwise, the output is null.
Sample input:
7
S 3
s 4
S 2
S 1
O
O
S 0
Sample output:
3
3
2
1
2
3
0
AC Code:
* * This program uses the array simulation stack */typedef int ELEMTYPE;
#define MAX 100000//Stack depth #include <stdio.h> #include <stdbool.h> int top =-1; /* Press the data in stack A on top of the stack index pointer (int *a,elemtype data) {if (top>=max-1 | top<-1) retur
n false;
A[++top] = data;
return true;
}/* The stack/bool Pop () {if (top<0) return False when the top index pointer is on the stack;
top--;
return true; }/* The current index pointer to the top of the stack is top,min array max depth also is Max, return to column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/and Min's valid element The number is the same as the number of elements in stack A, and its corresponding position is used to hold the minimum value of the stack a corresponding to the bottom part of the stack. */void Minall (int *a,int *min) {if (top>max-1) return
;
Min[0] = a[0];
int i;
for (i=1;i<=top;i++) {if (min[i-1] > A[i]) min[i] = A[i];
else min[i] = min[i-1];
}/* Returns the minimum value of the elements in the stack when top is on the stack * * min (int *min) {return min[top]; int main () {int n;
int A[max];
int Min[max];
while (scanf ("%d", &n)!= EOF) {int i;
for (i=0;i<n;i++) {char ci;
while (GetChar ()!= ' \ n ') continue;
scanf ("%c", &ci);
if (CI = = ' s ') {elemtype k;
scanf ("%d", &k);
Push (A,K);
} if (ci = = ' o ') {pop ();
} minall (A,min);
if (top<0) printf ("null\n");
else printf ("%d\n", Min (min));
} return 0; }
Author: csdn Blog Lan pavilion Wind and Rain