poj1011Sticks

package work;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Sticks {
static int N, total;
static int sti[];
static int len;
static boolean[] isV;

public static void main(String args[]) throws FileNotFoundException {
/* Scanner sc=new Scanner(System.in); */
Scanner sc = new Scanner(new File("src/1011.txt"));

while (true) {
int total = 0;
N = sc.nextInt();
if (N == 0) {
break;
}
sti = new int[N];
for (int i = 0; i < N; i++) {
sti[i] = sc.nextInt();
total += sti[i];
}
if (N == 1) {
System.out.println(sti[0]);
}
if (N == 2) {
if (sti[0] == sti[1]) {
System.out.println(sti[0]);
}
if (sti[0] != sti[1]) {
System.out.println(sti[0] + sti[1]);
}
}
if (N >= 3) {
Qsort(0, N - 1, sti);
System.out.println(Fuc());
}
}
}

private static int Fuc() {
// TODO Auto-generated method stub
for (len = sti[0]; len < total; len++) {
if (total / len != 0) {
continue;
}
isV = new boolean[len];
if (dfs(0, 0, 0))
break;
}
return len;
}

private static boolean dfs(int n, int p, int num) {//每条边完成长度,当前边位置,完成边数
// TODO Auto-generated method stub
if(len*num==total)return true;
for(int i=p;i<N;i++){
if(isV[i]){continue;}
if(n+sti[i]==len){
isV[i]=true;
if(dfs(0,i+1,num++)){return true;}
else{isV[i]=false;return false;}
}
if(n+sti[i]<len){
isV[i]=true;
if(dfs(n+sti[i],i+1,num)){return true;}
else{isV[i]=false;return false;}
}
if(sti[i]==0){return false;}
}
return false;
}

private static void Qsort(int l, int r, int[] ss) {
// TODO Auto-generated method stub
if (l < r) {
int key = ss[r];
int i = l;
int j = r;
while (i < j) {
while (i < j && ss[i] > key)
i++;
ss[j] = ss[i];
while (i < j && ss[j] <= key)
j--;
ss[i] = ss[j];
}
ss[j] = key;
Qsort(l, j - 1, ss);
Qsort(j + 1, r, ss);
}
}

}
package work;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Sticks {
 static int N, total;
 static int sti[];
 static int len;
 static boolean[] isV;

 public static void main(String args[]) throws FileNotFoundException {
  /* Scanner sc=new Scanner(System.in); */
  Scanner sc = new Scanner(new File("src/1011.txt"));

  while (true) {
   int total = 0;
   N = sc.nextInt();
   if (N == 0) {
    break;
   }
   sti = new int[N];
   for (int i = 0; i < N; i++) {
    sti[i] = sc.nextInt();
    total += sti[i];
   }
   if (N == 1) {
    System.out.println(sti[0]);
   }
   if (N == 2) {
    if (sti[0] == sti[1]) {
     System.out.println(sti[0]);
    }
    if (sti[0] != sti[1]) {
     System.out.println(sti[0] + sti[1]);
    }
   }
   if (N >= 3) {
    Qsort(0, N - 1, sti);
    System.out.println(Fuc());
   }
  }
 }

 private static int Fuc() {
  // TODO Auto-generated method stub
  for (len = sti[0]; len < total; len++) {
   if (total / len != 0) {
    continue;
   }
   isV = new boolean[len];
   if (dfs(0, 0, 0))
    break;
  }
  return len;
 }

 private static boolean dfs(int n, int p, int num) {//每条边完成长度,当前边位置,完成边数
  // TODO Auto-generated method stub
  if(len*num==total)return true;
  for(int i=p;i<N;i++){
   if(isV[i]){continue;}
   if(n+sti[i]==len){
    isV[i]=true;
    if(dfs(0,i+1,num++)){return true;}
    else{isV[i]=false;return false;}
   }
   if(n+sti[i]<len){
    isV[i]=true;
    if(dfs(n+sti[i],i+1,num)){return true;}
    else{isV[i]=false;return false;}
   }
   if(sti[i]==0){return false;}
  }
  return false;
 }

 private static void Qsort(int l, int r, int[] ss) {
  // TODO Auto-generated method stub
     if (l < r) {
         int key = ss[r];
         int i = l;
         int j = r;
         while (i < j) {
             while (i < j && ss[i] > key)
                 i++;
             ss[j] = ss[i];
             while (i < j && ss[j] <= key)
                 j--;
             ss[i] = ss[j];
         }
         ss[j] = key;
         Qsort(l, j - 1, ss);
         Qsort(j + 1, r, ss);
     }
 }

}

、、input

9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
2
1 1
2
1 3
0

、、output

4
1
4

、、output


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值