All pastes #1049696 Raw Edit

Asterix.java

public java v1 · immutable
#1049696 ·published 2008-06-18 00:31 UTC
rendered paste body
import java.util.*;import java.io.*;class Galois {	public int address;	public int transactions;		public void setAddress(int address) {		this.address = address;			}		public void setTransactions(int transactions) {		this.transactions = transactions;	}		public int getAddress() {		return address;	}		public int getTransactions() {		return transactions;	}	}class Obelix extends Galois {		public Obelix(int address, int transactions) {		setAddress(address);		setTransactions(transactions);	}		boolean transact(Obelix obelix ) {		return (transactions * obelix.transactions) < 0;	}		boolean isLarger(Obelix obelix) {		return (transactions > obelix.transactions);	}	}public class Asterix {		static int min(int a, int b) {		if (a < b) return a;		else return b;	}		public static void main(String[] argv) {		try {			File f = new File("asterix.input");			Scanner scn = new Scanner(f);			int trans,i,size,j;			int menir = 0;						size = scn.nextInt();			while (size != 0 ) {				Obelix[] array = new Obelix[size];				for (i = 0; i < size; i++) {					trans = scn.nextInt();					Obelix ovelix = new Obelix(i, trans);					array[i] = ovelix;				}			menir = 0;			for (i = 0; i < size; i++) {				for (j = 1; j < size; j++) {					if (array[i].transact(array[j])) {						if (array[i].isLarger(array[j])) {							int help = min(Math.abs(array[i].transactions), Math.abs(array[j].transactions));							menir += Math.abs(i-j) * help;							array[i].transactions -= help;							array[j].transactions += help;						}						else {							int help = min(Math.abs(array[i].transactions), Math.abs(array[j].transactions));							menir += Math.abs(i-j) * help;							array[i].transactions += help;							array[j].transactions -= help;									}					}				}			}			System.out.println(menir);			size = scn.nextInt();			}					} catch (IOException e) {			System.out.println("Error Message: " + e.getMessage());		}	}}