|
|
вернуться в форумTest 3# Why my code is wrong? Help me please!! import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n, total = 0, soma = 0, diferenca = 100001, pilha2 = 0; int weights[] = new int[20]; n = scan.nextInt(); if (n == 1) { System.out.println(scan.next()); return; } if (n == 2) { System.out.println(Math.abs(scan.nextInt() - scan.nextInt())); return; } for (int i = 0; i < n; i++) { weights[i] = scan.nextInt(); total += weights[i]; } for (int i = 0; i < n; i++) { soma = weights[i]; for (int j = 0; j < n; j++) { pilha2 = 0; if (i != j) { soma += weights[j]; pilha2 = total - soma; if (diferenca > (Math.abs(pilha2 - soma))) { diferenca = (Math.abs(pilha2 - soma)); } } } } System.out.println(diferenca); } } Re: Test 3# Послано Noob 23 янв 2012 10:23 Use bitmasks, greedy algorithm is wrong |
|
|