數目組合

package com.ccc.chess;
/**
 * 
 * @author Carol
 *
 */
public class ArrayComp {
 public int[] Combine(int[] a,int[] b){
  int temp=0;
  int temp_b=0;
  int temp_a=0;
  int aLenght=0;
  int bLenght=0;
  int cLength=0;
  if(a==null&&b==null){
   return null;
  }
  if(a!=null){
    aLenght=a.length;
  }
  if(b!=null){
   bLenght=b.length;
  }
  cLength=aLenght+bLenght;
  int[] c = new int[cLength];
  if(a.length==0){
   for(int i=0;i<b.length;i++){
    c[temp++]=b[i];
   }
   return c;
  }
  else if(b.length==0){
   for(int i=0;i<a.length;i++){
    c[temp++]=a[i];
   }
   return c;
  }
  for(int i=temp_a;i<a.length;i++){
    for(int j=temp_b;j<b.length;j++){
     System.out.println("==temp_b is"+temp_b+" a["+i+"] is"+a[i]+" b["+j+"] is"+b[j]);
     if(a[i]<=b[j]){
      c[temp]=a[i];
      System.out.println("找到A中的數值小于B中數值﹐將跳出B循環 。開始下個A的比較!! c["+temp+"] is "+c[temp]);
      temp++;
      temp_b=j;
      temp_a=i+1;
      if((i==a.length-1)&&j<b.length){//如果A循環完畢可是B還沒有循環完,就需要把B后面的元素接上
       for(int m=j;m<b.length;m++){
       c[temp++]=b[m];
       }
      }
      break;
     }
     else{
      temp_b=j+1;
      c[temp]=b[j];
      System.out.println("繼續循環在B中循環!! c["+temp+"] is "+c[temp]);
      temp++;
      if(j==b.length-1&&i<a.length){
       for(int m=i;m<a.length;m++){//如果B循環完畢可是A還沒有循環完,就需要把A后面的元素接上
        c[temp++]=a[m];
        }
      }
     
     
     }
    }
  }
  return c;
 }
 public static void main(String[] args) {
  ArrayComp ArrayComp = new ArrayComp();
  int[] a = new int[]{1,3,5,7,11};
  int[] b = new int[]{2,4,6,8,10};
  int [] c =ArrayComp.Combine(a, b);
  for(int i=0;i<c.length;i++){
   System.out.println("c["+i+"] 是"+c[i]);
  }
 }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值