背包思想

来源:互联网 发布:图片在数据库中的类型 编辑:程序博客网 时间:2024/04/27 15:16

package ss;
public class aa {

  /**
   * @param args
   */
  static int lenth = 5;
  static int T = 10;
  static int w[]= {2,4,6,8,10};
  static int already = 0;
  static int a[]= {0,0,0,0,0};
  public static void go(int i)
  {
   if(already == T)
   {
    for(int j = 0;j< lenth;j++)
    {
     System.out.print(a[j]+" ");
    }
    System.out.println();
   }
   else
   {
    for(int m = i;m < lenth;m++)
    {
     if(a[m] == 0 && already + w[m] <= T)
     {
      a[m] = 1;
      already += w[m];
      go(m+1);
      a[m] = 0;
      already -= w[m];
     }
    }
   }
  }
  public static void main(String[] args)
  {
   // TODO Auto-generated method stub
   go(0);
  }

 }

 

原创粉丝点击