62,620
社区成员
发帖
与我相关
我的任务
分享
import java.util.Arrays;
import java.util.Scanner;
public class Main
{
private static int part;
private static int max;
private static String[] ss;
private static int[] t;
private static boolean[] num;
private static int len;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
int sum = 0;
int n = Integer.parseInt(sc.nextLine());
if(n <= 0)
break;
String s = sc.nextLine();
ss = s.split(" ");
len = n;
num = new boolean[n];
t = new int[n];
for(int i = 0;i < ss.length;i++)
{
t[i] = Integer.parseInt(ss[i]);
sum += t[i];
}
Arrays.sort(t);
for(;n > 0;n--)
{
if(sum % n == 0)
{
part = n;
max = sum / part;
if(search())
{
System.out.println(max);
break;
}
}
}
}
}
public static boolean search()
{
int z = 0;
int sum = 0;
for(int k = len -1;k >= 0;k--)
{
int[] q = new int[len];
int i = 0;
if(t[k]>max)
return false;
if(t[k] == max)
{
num[k] = true;
z++;
continue;
}
if(num[k] == true)
continue;
sum = t[k];
for(int y = 0;y < k;y++)
{
if(num[y] == true)
{
continue;
}
sum += t[y];
q[i++] = y;
if(sum == max)
{
num[y] = true;
z++;
break;
}
else if(sum < max)
{
num[y] = true;
}
else
{
for(int p = 0;p < i;p++)
num[q[p]] = false;
i=0;
sum = t[k];
y--;
}
}
}
if(z == part)
return true;
else
return false;
}
}
public static void main(String[] args) {
stickLen(new int[]{5,2,1,5,2,1,5,2,1});
stickLen(new int[]{1,2,3,4});
}
static int stickLen(int[] parts) {
int total = 0;
int maxPart = 0;
for(int x : parts) {
total += x;
if(x > maxPart) maxPart = x;
}
for(int target=maxPart; target<=total; target++) {
if(total % target == 0) {
int[] sticks = new int[total/target];
if(partition(parts,sticks, target, 0)) {
System.out.println("result "+target);
return target;
}
}
}
return 0;
}
static boolean partition(int[] parts, int[] sticks, int target, int index) {
System.out.println(Arrays.toString(parts)+" "
+Arrays.toString(sticks)+" "+target+" "+index);
if(sticks[index]==target) index++;
if(index==sticks.length-1) {
int temp = 0;
for(int x : parts) temp += x;
return temp==target;
}
if(sticks[0] == 0) {
sticks[0] = parts[0];
parts[0] = 0;
return partition(parts, sticks, target, index);
}
for(int i=0; i<parts.length; i++) {
if(parts[i]==0 || parts[i]+sticks[index] > target) continue;
int temp = parts[i];
parts[i] = 0;
sticks[index] += temp;
if(partition(parts,sticks,target,index)) return true;
sticks[index] -= temp;
parts[i] = temp;
}
return false;
}
static int stickLen(int[] parts) {
int total = 0;
int maxPart = 0;
for(int x : parts) {
total += x;
if(x > maxPart) maxPart = x;
}
for(int target=maxPart; target<=total; target++) {
if(total % target == 0) {
int[] sticks = new int[total/target];
if(partition(parts,sticks, target, 0)) {
System.out.println("result "+target);
return target;
}
}
}
return 0;
}
static boolean partition(int[] parts, int[] sticks, int target, int index) {
System.out.println(Arrays.toString(parts)+" "+Arrays.toString(sticks)+" "+target+" "+index);
if(sticks[index]==target) index++;
if(index==sticks.length-1) {
int temp = 0;
for(int x : parts) temp += x;
return temp==target;
}
if(sticks[0] == 0) {
sticks[0] = parts[0];
parts[0] = 0;
return partition(parts, sticks, target, index);
}
for(int i=0; i<parts.length; i++) {
if(parts[i]==0 || parts[i]+sticks[index] > target) continue;
int temp = parts[i];
parts[i] = 0;
sticks[index] += temp;
if(partition(parts,sticks,target,index)) return true;
sticks[index] -= temp;
parts[i] = temp;
}
return false;
}
执行结果
[5, 2, 1, 5, 2, 1, 5, 2, 1] [0, 0, 0, 0] 6 0
[0, 2, 1, 5, 2, 1, 5, 2, 1] [5, 0, 0, 0] 6 0
[0, 2, 0, 5, 2, 1, 5, 2, 1] [6, 0, 0, 0] 6 0
[0, 0, 0, 5, 2, 1, 5, 2, 1] [6, 2, 0, 0] 6 1
[0, 0, 0, 5, 0, 1, 5, 2, 1] [6, 4, 0, 0] 6 1
[0, 0, 0, 5, 0, 0, 5, 2, 1] [6, 5, 0, 0] 6 1
[0, 0, 0, 5, 0, 0, 5, 2, 0] [6, 6, 0, 0] 6 1
[0, 0, 0, 0, 0, 0, 5, 2, 0] [6, 6, 5, 0] 6 2
[0, 0, 0, 5, 0, 0, 0, 2, 0] [6, 6, 5, 0] 6 2
[0, 0, 0, 5, 0, 0, 5, 0, 0] [6, 6, 2, 0] 6 2
[0, 0, 0, 5, 0, 1, 5, 0, 1] [6, 6, 0, 0] 6 1
[0, 0, 0, 0, 0, 1, 5, 0, 1] [6, 6, 5, 0] 6 2
[0, 0, 0, 0, 0, 0, 5, 0, 1] [6, 6, 6, 0] 6 2
result 6
[1, 2, 3, 4] [0, 0] 5 0
[0, 2, 3, 4] [1, 0] 5 0
[0, 0, 3, 4] [3, 0] 5 0
[0, 2, 0, 4] [4, 0] 5 0
[0, 2, 3, 0] [5, 0] 5 0
result 5