有数组b[] = {8,9,10,11,12,13,14,15,16,17,18,19},
数组x[] = {12,15,15,16,20,17,19,2,14,7,15,2,3,26,15,6,15,16,12,13,2,14,3}情况一;数组x[]的元素x[i]在数组b[]中,并且x[i+2]也在数组b[]中;
情况二:数组x[]的元素x[i]、x[i+2]、x[i+4]都在在数组b[]中;
情况三:数组x[]的元素x[i]、x[i+2]、x[i+4]、x[i+6]都在数组b[]中;求出算出数组x[]中有多少个情况一?多少个情况二?多少个情况三?高分求java代码撒,各位大虾写的越详细越好撒,看不懂的不给分撒,越简洁越好撒。

解决方案 »

  1.   

    for example int b[] = {8,9,10,11,12,13,14,15,16,17,18,19};
    int x[] = {12,15,15,16,20,17,19,2,14,7,15,2,3,26,15,6,15,16,12,13,2,14,3};
    String s = Arryas.toString(b);
    int cnt[] = {0, 0, 0};
    for (int i=0; i+2<x.length; i++) {
        if (s.indexOf(x[i]+"")>0 && s.indexOf(x[i+2]+"")>0) cnt[0]++;
    }
    for (int i=0; i+4<x.length; i++) {
        if (s.indexOf(x[i]+"")>0 && 
            s.indexOf(x[i+2]+"")>0 &&
            s.indexOf(x[i+4]+"")>0) cnt[1]++;
    }
    for (int i=0; i+6<x.length; i++) {
        if (s.indexOf(x[i]+"")>0 && 
            s.indexOf(x[i+2]+"")>0 &&
            s.indexOf(x[i+4]+"")>0 &&
            s.indexOf(x[i+6]+"")>0) cnt[2]++;
    }
    for (int i=0; i<cnt.length; i++) {
        System.out.printf("情况%d:%d\n", (i+1), cnt[i]);
    }
      

  2.   

    楼上答案好像不对嘛,看看这个 int b[] = { 8,9,10,11,12,13,14,15,16,17,18,19 };
    int x[] = {12,15,15,16,20,17,19,2,14,7,15,2,3,26,15,6,15,16,12,13,2,14,3};
    List<Integer> s = new ArrayList<Integer>();
    for (int i: b) s.add(i);
    int cnt[] = { 0, 0, 0 };
    for (int i = 0; i + 2 < x.length; i++) {
    if (s.contains(x[i] )  && s.contains(x[i + 2] ))
    cnt[0]++;
    }
    for (int i = 0; i + 4 < x.length; i++) {
    if (s.contains(x[i] )  && s.contains(x[i + 2] )
    && s.contains(x[i + 4] ))
    cnt[1]++;
    }
    for (int i = 0; i + 6 < x.length; i++) {
    if (s.contains(x[i] )  && s.contains(x[i + 2] )
    && s.contains(x[i + 4] )&&s.contains(x[i + 6] ))
    cnt[2]++;
    }
    for (int i = 0; i < cnt.length; i++) {
    System.out.printf("情况%d:%d\n", (i + 1), cnt[i]);
    }
      

  3.   

    因为这三种情况是互相包含的,即,只有条件1成立,条件2才可能成立; 条件2成立,条件3才可能成立 private static String strB = null;
    private static boolean[] flagB = null; /**
     * @param args
     */
    public static void main(String[] args) {
    int b[] = { 8,9,10,11,12,13,14,15,16,17,18,19 };
            int x[] = {12,15,15,16,20,17,19,2,14,7,15,2,3,26,15,6,15,16,12,13,2,14,3};        initArrayB2(b);
            
            int c1 =0, c2 =0, c3 =0;
            for(int i =0; i<x.length-2;i++){
             if(inB(x[i]) && inB(x[i+2])){
             c1++;
             if(i<x.length-4 && inB(x[i+4])){
             c2++;
             if(i < x.length-6 && inB(x[i+6])){
             c3++;
             }
             }
             }
            }
            
            System.out.printf("Has %d condition 1, %d condition 2 and %d condition 3\n", c1,c2,c3);
    } // 如果B是没有规律的,用string来做
    private static void initArrayB1(int[] b) {
    StringBuffer buf = new StringBuffer();
    buf.append(',');
    for(int a: b){
    buf.append(a).append(',');
    }
    strB = buf.toString();
    }

    // 如果B是有规律的,用flag 数组,或者bitmap都可以,这里用数组
    private static void initArrayB2(int[] b) {
    flagB = new boolean[b[b.length-1]+1];
    for(int i=0;i<b.length;i++){
    flagB[b[i]] = true;
    }
    }

    private static boolean inB(int i) {
    if(strB != null){
    return (strB.indexOf(","+i+",") >= 0);
    }else{
    if (0<=i && i < flagB.length)
    return flagB[i];
    else
    return false;
    }
    }
      

  4.   


    public class Array {
    private static int num = 4;//情况的种数
    public static void main(String[] args) {

    int b[] = {8,9,10,11,12,13,14,15,16,17,18,19};
    int x[] = {12,15,15,16,20,17,19,2,14,7,15,2,3,26,15,6,15,16,12,13,2,14,3};
    int[] count = new int[num];//存放每种情况的次数

    //查找,可以是三中情况,也可以是更多种
    for(int i=0; i<num; i++) {
    count[i] = find(x, b, i+1);
    }
    for(int i=0; i<num; i++) {
    System.out.println("情况" + i + ":" + count[i]);
    }
    }

    /**
     * 查找数组中是否存在某个数
     * @param arr 要查找的数组,比方说上面的b
     * @param num 要查的数字
     * @return true,表示数组中存在要找的数字,否则就是没找到
     */
    private static boolean exists(int[] arr, int num) {
    for(int i=0; i<arr.length; i++) {
    if(arr[i] == num) {
    return true;
    }
    }
    return false;
    }

    /**
     * 处理每种情况
     * @param arr1 子数组,比方说上面的x
     * @param arr2 主数组,比方说上面的b
     * @param num 对应各种情况,1为情况1,一次类推
     */
    private static int find(int[] arr1, int[] arr2, int num) {
    int count = 0;
    if(num <= 0 || num > arr1.length) {
    System.out.println("请输入合适的参数!");
    return -1;
    }
    for(int i=0; i<arr1.length - num * 2; i++) {
    boolean flag = true;
    for(int j=0; j<=num; j++) {
    if(!exists(arr2, arr1[i + j * 2])) {
    flag = false;
    continue;
    }
    }
    if(flag) {
    count ++;
    }
    }
    return count;
    }
    }刚才实现了一个,效率可能不高,但是扩展性好!可以按照这个规律扩展成更多种情况。
      

  5.   

    这个
    Java code        int b[] = { 8,9,10,11,12,13,14,15,16,17,18,19 };
            int x[] = {12,15,15,16,20,17,19,2,14,7,15,2,3,26,15,6,15,16,12,13,2,14,3};
            List<Integer> s = new A……
    [/Quote]
      

  6.   


    public static void main(String[] args) {
    int b[] = { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
    int x[] = { 8, 3, 10, 91, 12, 11, 14, 3, 16, 13, 14, 15, 16, 17, 18, 19 };
    array(b, x, 6);
    array(b, x, 4);
    array(b, x, 2);
    }
    /**
     * 
     * @param small 小数组
     * @param big 大数组
     * @param length 2,4,6
     */
    private static void array(int[] small, int[] big, int length) {
    //放相等结果集
    int[] res = new int[small.length];
    for (int i = 0; i < small.length; i++) {
    if (small[i] == big[i]) {
    res[i] = 1;//相等为1
    }
    }
    //情况变量
    int[] lens = new int[length / 2];
    int count = 0;
    do {
    lens[count] = length;
    count++;
    length -= 2;
    } while (length != 0);
    check(res, lens);
    }
    /**
     * 
     * @param res结果集
     * @param lens 变量数组
     */
    private static void check(int[] res, int[] lens) {
    Map map = new HashMap();
    for (int i = 0; i < lens.length; i++) {
    for (int j = 0; j < res.length - lens[i]; j++) {
    if (res[j] == 1 && res[j + lens[i]] == 1) {
    String key = lens[i] + "";
    if (map.containsKey(key)) {
    map.put(key, Integer.valueOf(map.get(key).toString())
    .intValue()
    + 1 + "");
    } else {
    map.put(key, "1");
    }
    }
    }
    }
    System.out.println(map.toString());
    }
    我也来个
      

  7.   

    高效的: int b[]={8,9,10,11,12,13,14,15,16,17,18,19};
    int x[]={12,15,15,16,20,17,19,2,14,7,15,2,3,26,15,6,15,16,12,13,2,14,3};
    Arrays.sort(b); for(int i=0;i < x.length-2;i++){
    if(Arrays.binarySearch(b,x[i])>=0 && Arrays.binarySearch(b,x[i+2])>=0){
    System.out.println("情况1:"+i);
    }
    }
    for(int i=0;i < x.length-4;i++){
    if(Arrays.binarySearch(b,x[i])>=0 && Arrays.binarySearch(b,x[i+2])>=0 && Arrays.binarySearch(b,x[i+4])>=0){
    System.out.println("情况2:"+i);
    }
    }
    for(int i=0;i < x.length-6;i++){
    if(Arrays.binarySearch(b,x[i])>=0 && Arrays.binarySearch(b,x[i+2])>=0 && Arrays.binarySearch(b,x[i+4])>=0 && Arrays.binarySearch(b,x[i+6])>=0){
    System.out.println("情况3:"+i);
    }
    }
      

  8.   

    我也来试试: static boolean isIn(int[] arr, int s){
    for(int i = 0; i < arr.length; ++i){
    if(arr[i] == s)
    return true;
    }
    return false;
    }

    static void testArray()
    {
    int b[] = {8,9,10,11,12,13,14,15,16,17,18,19};
    int x[] = {12,15,15,16,20,17,19,2,14,7,15,2,3,26,15,6,15,16,12,13,2,14,3}; //r[0] for case 1
    //r[1] for case 2
    //r[3] for case 3
    int r[] = {0,0,0};

    for(int i = 0; i < x.length - 2; ++i){
    if(isIn(b, x[i])){
    for(int j = 0; j < r.length; ++j){
    if(i < x.length - 2*(j+1)){
    if(isIn(b, x[i+2*(j+1)]))
    ++r[j];
    else
    break;
    }
    }
    }
    }

    System.out.println("case 1 is:"+r[0]+",case 2 is:"+r[1]+",case 3 is:"+r[2]);
    }
            
            static void main(String[] args){
                 testArray();
            }
      

  9.   

    上面注释有个笔误,r[2] for case 3