日期:2014-05-20  浏览次数:20662 次

有有一个数学问题,请大家指教。高分哦
两个二维数组,怎样判断它们两个互不包容。
也就是说:它们两个中不包含各自的元素。
(例如)
a二维数组中有
{1,2,3,4,5,6}
{3,45,5,6,7,44}
b二维数组中有
{33,56,77,9,10,80}
{6,8,9,45,88,90}
c二维数组中有
{122,55,67,99,80,199}
{777,595,97,234,898,102}
a跟b有包容元素,返回false
a跟c无包容元素,返回true


------解决方案--------------------
public class D {
public static void main(String[] args) {
int[][] a = { {1,2,3,4,5,6},
{3,45,5,6,7,44}} ;
int[][] b = {{33,56,77,9,10,80} ,
{11,8,9,45,88,90}};

System.out.println(bijiao(a,b));

}

public static boolean bijiao(int x[][],int y[][]) {
boolean z = true;
for(int i=0;i<2;i++) {
for(int j=0;j<6;j++) {
for(int a=0;a<2;a++) {
for(int b=0;b<6;b++) {
if(x[i][j] == y[a][b]) {
z = false;
break;
}



}
}
}
}

return z;

}
}

------解决方案--------------------
Java code

/**
 * 
 */
package houlei.test;

import java.util.Arrays;

/**
 * 该类创建于 2008-8-29 下午04:19:04
 * 
 * @version 1.0.0
 * @author 侯磊
 */
public class E {

    public static void main(String[] args) {
        int a[][] = new int[][] { { 1, 2, 3, 4, 5, 6 }, { 3, 45, 5, 6, 7, 44 } };
        int b[][] = new int[][] { { 33, 56, 77, 9, 10, 80 }, { 6, 8, 9, 45, 88, 90 } };
        int c[][] = new int[][] { { 122, 55, 67, 99, 80, 199 }, { 777, 595, 97, 234, 898, 102 } };
        System.out.println(dissimilitude(a, b));
        System.out.println(dissimilitude(a, c));
    }

    public static boolean dissimilitude(int a[][], int b[][]) {
        int ta[] = sort(a);
        int tb[] = sort(b);
        int i = 0, j = 0;
        while (i < ta.length && j < tb.length) {
            if (ta[i] == tb[j])
                return false;
            else if (ta[i] < tb[j])
                i++;
            else
                j++;
        }
        return true;
    }

    public static int[] sort(int a[][]) {
        int t[] = new int[a.length * a[0].length];
        int index = 0;
        for (int i = 0; i < a.length; i++)
            for (int j = 0; j < a[0].length; j++) {
                t[index++] = a[i][j];
            }
        Arrays.sort(t);
        return t;
    }
}

------解决方案--------------------
Java code


import java.util.Set;
import java.util.HashSet;

public class Test10 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        int[][] a = {
                {1, 2, 3, 4, 5, 6},
                {3, 45, 5, 6, 7, 44}
        };
        
        int[][] b = {
                {33, 56, 77, 9, 10, 80},
                {6, 8, 9,45, 88, 90}
        };
        
        int[][] c = {
                {122, 55, 67, 99, 80, 199},
                {777, 595, 97, 234, 898, 102}
        };

        if (isContained(a, b)) {
            System.out.println("The array B contains some same elements with A");
        } else {        
            System.out.println("The array B does not contain some same elements with A");
        }
        
        if (isContained(a, c)) {
            System.out.println("The array C contains some same elements with A");
        } else {        
            System.out.println("The array C does not contain some same elements with A");
        }
    }
    
    public static boolean isContained(int[][] a, int[][] b) {
        Set<Integer> set = new HashSet<Integer>();
        
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                set.add(a[i][j]);
            }
        }
        
        for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < b[i].length; j++) {
                if (set.contains((Integer)b[i][j])) {
                    return true;
                }                    
            }
        }
        return false;
    }

}