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

用数组为什么内存超呢?

Posted by wskiwwwx at 2009-08-06 14:30:09 on Problem 1182
import java.util.Scanner;

public class Main {

	private static int[][] a = new int[100000][2];// 存D关系为1的
	private static int[][] b = new int[100000][2];// 存关系为2的
	private static int i = 0;// 表示a实际的元素个数
	private static int j = 0;// 表示b实际的元素个数
	private static int sum = 0;// 存假话的个数

	public static void insertA(int v1, int v2) {
		a[i][0] = v1;
		a[i][1] = v2;
		i++;
	}

	public static void insertB(int v1, int v2) {
		b[j][0] = v1;
		b[j][1] = v2;
		j++;
	}

	public static void getSum(int n, int d, int v1, int v2) {
		if (v1 > n || v2 > n) {
			sum++;
			return;
		} else if (d == 2 && v1 == v2) {
			sum++;

			return;
		} else if (d == 1) {
			for (int k = 0; k < j; k++) {
				if ((b[k][0] == v1 && b[k][1] == v2)
						|| (b[k][0] == v2 && b[k][1] == v1)) {
					sum++;
					return;
				}
			}
		} else if (d == 2) {
			for (int k = 0; k < j; k++) {
				if (b[k][0] == v2 && b[k][1] == v1) {
					sum++;
					return;
				}
			}
			for (int k = 0; k < i; k++) {
				if ((a[k][0] == v1 && a[k][1] == v2)
						|| (a[k][0] == v2 && a[k][1] == v1)) {
					sum++;
					return;
				}
			}
		}
		// 将正确的话分别分类存入A和B数组
		if (d == 1) {
			if (v1 != v2)
				insertA(v1, v2);
		} else if (d == 2) {
			insertB(v1, v2);
		}

	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		for (int i = 0; i < k; i++) {
			int d = sc.nextInt();
			int v1 = sc.nextInt();
			int v2 = sc.nextInt();
			Main.getSum(n, d, v1, v2);
		}
		System.out.println(Main.sum);
	}
}

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