Created
November 23, 2012 09:20
-
-
Save itkovian/4134704 to your computer and use it in GitHub Desktop.
Vals geld
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package valsgeld; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Scanner; | |
import java.util.TreeSet; | |
// spaces instead of tabs. | |
public class Main { | |
public static void main(String[] args){ | |
int n, length; // separate lines is better | |
String[] weging; | |
List<String[]> geldpakket; | |
List<List<String[]>> lijstVanPakketten; | |
Scanner input = new Scanner(System.in); | |
StringBuffer evenwicht, zwaarder, lichter; | |
// Try to use interfaces rather than fixed classes. That way, you are always safe, as long as there's something that implements this interface. | |
// You were doing that with List afaik. | |
TreeSet<Character> result = new TreeSet<Character>(); | |
boolean licht = false, zwaar = false; // Put these on two separate lines. | |
n = input.nextInt(); | |
lijstVanPakketten = new ArrayList<List<String[]>>(n); // Just a simple array suffices, you're indexing anyway. | |
for(int i=0;i<n;i++){ // spaces around operators and after ; | |
length = input.nextInt(); | |
geldpakket = new ArrayList<String[]>(length); // Just a simple array suffices, I think, since you know the length/size. | |
for(int j=0;j<length;j++){ | |
weging = new String[3]; | |
for(int k=0;k<3;k++){ | |
weging[k] = input.next(); | |
} | |
geldpakket.add(weging); | |
} | |
lijstVanPakketten.add(geldpakket); // This looks odd, given that you give it a fixed size above | |
} | |
for(int i=0;i<lijstVanPakketten.size();i++){ | |
// implement the below in a separate method. You call that method per case. Makes it more clear what is going on | |
// also call it something else, lijstVanPakketten seems awkward, since it's not really true, it's lijstvanlijstvanpakketten | |
evenwicht = new StringBuffer(); | |
lichter = new StringBuffer(); | |
zwaarder = new StringBuffer(); | |
for(int j=0;j<lijstVanPakketten.get(i).size();j++){ | |
if(lijstVanPakketten.get(i).get(j)[2].equals("evenwicht")) { | |
evenwicht.append(lijstVanPakketten.get(i).get(j)[0]); // write a separate method that does the get(i).get(j), see blood example | |
evenwicht.append(lijstVanPakketten.get(i).get(j)[1]); | |
} | |
if(lijstVanPakketten.get(i).get(j)[2].equals("omhoog")) { | |
zwaarder.append(lijstVanPakketten.get(i).get(j)[0]); | |
lichter.append(lijstVanPakketten.get(i).get(j)[1]); | |
} | |
if(lijstVanPakketten.get(i).get(j)[2].equals("omlaag")) { | |
zwaarder.append(lijstVanPakketten.get(i).get(j)[1]); | |
lichter.append(lijstVanPakketten.get(i).get(j)[0]); | |
} | |
} | |
if(evenwicht.length()!=0){ | |
String s = evenwicht.toString(); | |
if(lichter.length()!=0) | |
for(int k=0;k<lichter.length();k++){ | |
if(s.indexOf(lichter.charAt(k))==-1) { | |
result.add(lichter.charAt(k)); | |
licht = true; | |
} | |
if(s.indexOf(zwaarder.charAt(k))==-1) { | |
result.add(zwaarder.charAt(k)); | |
zwaar = true; | |
} | |
} | |
} | |
else { | |
//System.out.println("leeg evenwichtsbuffer"); | |
// The below is confusing :-) | |
Map<Character, Integer> map = new HashMap<Character, Integer>(); | |
//controleer stringbuffer lichter | |
for(int j=0;j<lichter.length();j++){ | |
if(!map.containsKey(lichter.charAt(j))) map.put(lichter.charAt(j), 1); | |
else {map.put(lichter.charAt(j), map.get(lichter.charAt(j))+1);} | |
} | |
//controleer stringbuffer zwaarder | |
for(int j=0;j<zwaarder.length();j++){ | |
if(!map.containsKey(zwaarder.charAt(j))) map.put(zwaarder.charAt(j), 1); | |
else {map.put(zwaarder.charAt(j), map.get(zwaarder.charAt(j))+1);} | |
} | |
Collection c = map.values(); | |
int max = Collections.max(c); | |
for(int j=0;j<zwaarder.length();j++){ | |
if(map.get(zwaarder.charAt(j)) == max){ | |
result.add(zwaarder.charAt(j)); | |
zwaar=true; | |
} | |
if(map.get(lichter.charAt(j)) == max){ | |
result.add(lichter.charAt(j)); | |
licht=true; | |
} | |
} | |
} | |
if(result.size() == 1 && licht != zwaar){ | |
if(licht) System.out.println("Het valse geldstuk " +result.first()+ " is lichter."); | |
else System.out.println("Het valse geldstuk " +result.first()+ " is zwaarder."); | |
} | |
else if(lijstVanPakketten.get(i).size() == 1 || zwaar != licht) System.out.println("Te weinig gegevens."); | |
else System.out.println("Inconsistente gegevens."); | |
result.clear(); | |
licht = false; | |
zwaar = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment