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

JAVA Why is not right

Posted by 350053879 at 2009-08-26 01:49:23 on Problem 1094
package acm.pku.edu.fishcanfly;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
	private static int n;

	private static int m;

	private static boolean map[][] = new boolean[27][27];

	private static int res = -1;
	
	private static int step = 0;

	private static char[] ans = new char[26];

	public static void main(String[] args) {
		String s = null;
		while (true) {

			Scanner in = new Scanner(System.in);
			n = in.nextInt();
			m = in.nextInt();

			if (n == 0) {
				break;
			}

			initMap();

			BufferedReader in2 = new BufferedReader(new InputStreamReader(
					System.in));

			for (int cnt = 1; cnt <= m; cnt++) {
				try {
					s = in2.readLine();
				} catch (IOException e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
				}
				parseS(s);

				if (res != 0) {
					continue;
				}

				if ((res = dealResult()) != 0) {
					step = cnt;
				}

			}

			if (res == 0) {
				System.out.println("Sorted sequence cannot be determined.");
			}
			else if(res == 2){
				System.out.println("Inconsistency found after " + step
					+ " relations.");
			} else {
				System.out.println("Sorted sequence determined after "
						+ step + " relations: " + ans.toString() + ".");
			}

		}
	}

	private static int dealResult() {
		int i, j, k;
		// TODO 自动生成方法存根
		for (i = 0; i < n; i++) {
			for (j = 0; j < n; j++) {
				for (k = 0; k < n; k++) {
					if (map[i][k] && map[k][j]) {
						map[i][j] = true;
					}
				}
			}
		}

		for (i = 0; i < n; i++) {
			for (j = i; j < n; j++) {
				if (map[i][j] && map[j][i]) {
					return 2;
				}
			}
		}

		for (i = 0; i < n; i++) {
			for (j = i + 1; j < n; j++) {
				if ((!map[i][j] && !map[j][i])) {
					return 0;
				}
			}
		}
        char ch = 'A';
        String seq = null;
		for (i = 0; i < n; i++) {
			int c = 0;
			for (j = 0; j < n; j++) {
				if (map[i][j]) {
					c++;
				}			
			}
			ans[(n - 1) - c] = ch;
			ch++;
		}
		ans[n] = '\0';
		seq = ans.toString();
		

		return 1;
	}

	private static void parseS(String s) {
		// TODO 自动生成方法A存根
		map[s.charAt(0) - 'A'][s.charAt(2) - 'A'] = true;

	}

	private static void initMap() {
		ans = new char[26];
		res = 0;
		// TODO 自动生成方法存根
		for (int i = 0; i < 26; i++) {
			for (int j = 0; j < 26; j++) {
				map[i][j] = false;
			}
		}

	}

}

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