First, solve the problem. Then, write the code. ” - John Johnson

It's not at all important to get it right the first time. It's vitally important to get it right the last time. ” - Andrew Hunt and David Thomas

Tuesday, July 15, 2014

Vito's Familly

Name of programmer: Jisan
Programming Language: Java
Difficulty: Interesting.. :P
Problem number: UVA-10041
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=982

Help: http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html

Code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Uva_10041 {
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner in=new Scanner(System.in);
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
int counter=Integer.parseInt(buff.readLine());
StringTokenizer jis;
for(int i=0;i<counter;i++){
jis=new StringTokenizer(buff.readLine());
int k=Integer.parseInt(jis.nextToken());
int[] tomato=new int[k];
for(int j=0;j<tomato.length;j++){
tomato[j]=Integer.parseInt(jis.nextToken());
}
int ans=(1<<23);
for(int m=0;m<tomato.length;m++){
int temp=0;
for(int n=0;n<tomato.length;n++){
if(m!=n){
temp =temp+ (Math.abs(tomato[n] - tomato[m]));
}
}
ans=Math.min(ans, temp);
}
System.out.println(ans);
}
}
}

No comments:

Post a Comment