UVA 591 - Box of Bricks solution in java
Posted by Unknown on Friday, August 14, 2015 with No comments
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner input = null;
try {
input = new Scanner(System.in);
int testCase = input.nextInt();
int set = 1;
while (testCase != 0) {
int total = 0;
int[] ara = new int[testCase];
for (int i = 0; i < testCase; i++) {
ara[i] = input.nextInt();
total = total + ara[i];
}
int avgBricks = total / testCase;
int totalMove = 0;
Arrays.sort(ara);
for (int i = 0; i < testCase; i++) {
if (ara[i] > avgBricks) {
int move = ara[i] - avgBricks;
totalMove = move + totalMove;
}
}
System.out.println("Set #" + set);
System.out.println("The minimum number of moves is " + totalMove + ".");
System.out.println();
set++;
testCase = input.nextInt();
}
} finally {
if (input != null)
input.close();
}
}
}
coded by sudipto
import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner input = null;
try {
input = new Scanner(System.in);
int testCase = input.nextInt();
int set = 1;
while (testCase != 0) {
int total = 0;
int[] ara = new int[testCase];
for (int i = 0; i < testCase; i++) {
ara[i] = input.nextInt();
total = total + ara[i];
}
int avgBricks = total / testCase;
int totalMove = 0;
Arrays.sort(ara);
for (int i = 0; i < testCase; i++) {
if (ara[i] > avgBricks) {
int move = ara[i] - avgBricks;
totalMove = move + totalMove;
}
}
System.out.println("Set #" + set);
System.out.println("The minimum number of moves is " + totalMove + ".");
System.out.println();
set++;
testCase = input.nextInt();
}
} finally {
if (input != null)
input.close();
}
}
}
coded by sudipto
Categories: programming, Uva solve
0 comments:
Post a Comment