Skip to content

Instantly share code, notes, and snippets.

@them0nk
Created April 30, 2012 01:44
Show Gist options
  • Save them0nk/2554789 to your computer and use it in GitHub Desktop.
Save them0nk/2554789 to your computer and use it in GitHub Desktop.
The Race: PC/UVa IDs: 110103/10137
/*
* Main.java
* java program model for www.programming-challenges.com
*/
import java.io.*;
import java.util.*;
class Main implements Runnable{
public static void main(String args[]) // entry point from OS
{
Main myWork = new Main(); // Construct the bootloader
myWork.run(); // execute
}
public void run() {
new Q103().run();
}
}
class Q103 implements Runnable{
public int min_amount_exchange(int num_students, int amount_spent[])
{
int total_amount = 0;
int average_amount = 0;
for(int i=0;i<num_students;i++)
{
total_amount += amount_spent[i];
}
average_amount = total_amount / num_students;
int amount_to_be_given = 0;
int amount_to_be_received = 0;
for(int i=0;i<num_students;i++)
{
if (amount_spent[i] < average_amount )
{
amount_to_be_given += (average_amount - amount_spent[i]);
}
else if (amount_spent[i] > average_amount )
{
amount_to_be_received += (amount_spent[i] - average_amount -1);
}
}
if (amount_to_be_given > amount_to_be_received)
return amount_to_be_given;
else
return amount_to_be_received;
}
public void run(){
try
{
int num = 1;
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
String inputLine;
while ((inputLine = is.readLine( )) != null) {
int num_students = Integer.parseInt(inputLine.trim());
if (num_students == 0)
break;
int amount_spent[] = new int[num_students];
int exchange_amount = 0;
for(int i=0 ; i<num_students; i++)
{
inputLine = is.readLine( );
String a[] = inputLine.split("\\.");
int b = Integer.parseInt(a[0])*100;
int c = Integer.parseInt(a[1]);
amount_spent[i] = b+c;
}
exchange_amount = min_amount_exchange(num_students, amount_spent);
System.out.printf("$%d.%02d\n",exchange_amount/100,exchange_amount%100);
}
is.close( );
} catch (Exception e) {
System.out.println(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment