第一题:新建两个栈用来保存最小最大值就可以啦 package test.wangmei;
import java.util.Scanner;
import java.util.Stack;
class MyStack{
private Stack<Integer> stack;
private Stack<Integer> minStack;
private Stack<Integer> maxStack;
public MyStack() {
stack = new Stack<>();
minStack = new Stack<>();
maxStack = new Stack<>();
}
public void push(int num) {
stack.push(num);
if (minStack.isEmpty() || minStack.peek() >= num)
minStack.push(num);
if (maxStack.isEmpty() || maxStack.peek() <= num)
maxStack.push(num);
}
public int peek() {
return stack.peek();
}
public int pop() {
int num = stack.pop();
if (!minStack.isEmpty() && minStack.peek() == num)
minStack.pop();
if (!maxStack.isEmpty() && maxStack.peek() == num)
maxStack.pop();
return num;
}
public int min() {
return minStack.peek();
}
public int max() {
return maxStack.peek();
}
}
public class Main {
public static void main(String[] args) {
MyStack stack ;
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
stack = new test.wangmei.MyStack();
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
stack.push(scanner.nextInt());
}
System.out.println(stack.max()+","+stack.min());
}
}
}