Problem A – Grouping Problem

import java.util.*;
import java.io.*;

class Main
{
	public static void main( String[] args ) throws Exception
	{
		BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );

		int N, count, sum, temp;
		int[] data, data2;

		N = Integer.parseInt( br.readLine() );

		while( N != 0 )
		{
			data = new int[N];
			data2 = new int[(int)Math.pow(2,N)-1];
			count = 0;
			sum = 0;
			temp = 0;

			for( int i = 0; i < N; i++ )
			{
				data&#91;i&#93; = Integer.parseInt( br.readLine() );
				sum += data&#91;i&#93;;
			}

			for( int i = 0; i < N; i++ )
			{
				int count2 = count;

				for( int j = 0; j < count2; j++ )
				{
					data2&#91;count++&#93; = data2&#91;j&#93; + data&#91;i&#93;;
				}

				data2&#91;count++&#93; = data&#91;i&#93;;
			}

			Arrays.sort( data2 );

			for( int i = 0; i < data2.length; i++ )
			{
				temp = i;
				if( (double) data2&#91;i&#93; >= (double) sum / 2.0 ) break;
			}

			if( (double) data2[temp] == (double) sum / 2.0 ) System.out.println( "0" );
			else System.out.println( data2[temp] - data2[temp-1] );

			N = Integer.parseInt( br.readLine() );
		}
	}
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: