Skip to content

Instantly share code, notes, and snippets.

@KazumasaSUGAYA
Created October 24, 2012 13:29
Show Gist options
  • Save KazumasaSUGAYA/3946059 to your computer and use it in GitHub Desktop.
Save KazumasaSUGAYA/3946059 to your computer and use it in GitHub Desktop.
Stacking Blocks II
【引用先】
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=10033
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class StackingBlock {
/**
* @param args
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException,
IOException {
List<Stack<String>> stack = new ArrayList<Stack<String>>();
makeStack(stack);
System.out.println(" コマンド(push or pop)を入力してください。 : ");
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);
while (true) {
String command = br.readLine();
if (command.equals("quit")) {
break;
}
String[] order = command.split(" ");
String ord = order[0];
int p = Integer.parseInt(order[1]) - 1;
if (command.equals("push")) {
stack.get(p).push(command);
} else if (command.equals("pop")) {
// st.pop();
if (!(stack.get(p).empty())) {
System.out.println(stack.get(p).pop());
} else {
System.out.println("stack is empty");
}
} else {
break;
}
}
}
private static void makeStack(List<Stack<String>> stacks)
throws IOException {
System.out.println(" 何個の山を作りますか? 100まで: ");
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);
int num_stack = Integer.parseInt(br.readLine());
// num_stack個のstackを作成する。
for (int i = 0; i < num_stack; i++) {
stacks.add(new Stack<String>());
}
System.out.println(num_stack + "個の山を作りました。");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment