Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

这道题注意理解题意,贴个AC了的代码

Posted by wskiwwwx at 2010-08-15 17:24:21 on Problem 1363
import java.util.Scanner;
import java.util.Stack;

public class Main{

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			if (n == 0)
				break;
			while (sc.hasNext()) {
				int[] seq = new int[n + 1];
				seq[1] = sc.nextInt();
				if (seq[1] == 0)
					break;
				for (int i = 2; i <= n; ++i)
					seq[i] = sc.nextInt();
				slove(seq, n);
			}
			System.out.println();
		}
	}

	static void slove(int[] seq, int n) {
		Stack<Integer> stack = new Stack<Integer>();
		int j = 1;
		for (int i = 1; i <= n; ++i) {
			stack.push(i);
			if (i == seq[j]) {
				stack.pop();
				j++;
				while (!stack.isEmpty()) {
					int x = stack.pop();
					if (seq[j] != x){
						stack.push(x);//如果此数不相等,必须要再push进栈
						break;
					}
					j++;
				}
			}
		}
		if (j == n+1)
			System.out.println("Yes");
		else
			System.out.println("No");
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator