View Code of Problem 7

package chapter1;

import java.util.Scanner;

public class NightFury {
	static Scanner in = new Scanner(System.in);

	public static int flag(String a) {
		char A[] = a.toCharArray();
		for (int i = 0; i < A.length; i++) {
			if (A[i] != '0') {
				return i;
			}
		}
		return -1;
	}

	public static int change_System(String a) {
		char A[] = a.toCharArray();
		int temp = 1, sum = 0;
		for (int i = A.length - 1; i >= flag(a); i--) {
			sum += (A[i] - 48) * temp;
			temp *= 10;
		}
		return sum;
	}

	public static int search(int A[][], int a) {
		for (int i = 0; i < A.length; i++) {
			if (A[i][0] == a) {
				return A[i][1];
			}
			if (A[i][1] == a) {
				return A[i][0];
			}
		}
		return -1;
	}

	public static void main(String[] args) {
		int T = in.nextInt();
		for (int i = 0; i < T; i++) {
			int N = in.nextInt();
			int A[][] = new int[1000000][2];
			for (int j = 0; j < N; j++) {
				String a = in.next();
				String b = a.substring(0, a.indexOf("="));
				String c = a.substring(a.indexOf("=") + 1, a.length());
				A[j][0] = change_System(b);
				A[j][1] = change_System(c);
			}
			int M = in.nextInt();
			for (int t = 0; t < M; t++) {
				int a = in.nextInt();
				if (search(A, a) != -1) {
					System.out.println(search(A, a));
				} else
					System.out.println("UNKNOW");
			}
			if (i != T - 1)
				System.out.println();
		}
	}
}
/*
Main.java:5: error: class NightFury is public, should be declared in a file named NightFury.java
public class NightFury {
       ^
1 error
*/

Double click to view unformatted code.


Back to problem 7