Anyways, this is my newest code (with the normal standard input this time)
- Code: Select all
import java.io.*;
import java.util.*;
public class Main implements Comparable<Main>{
public String name, reducedName;
public int points, wins, losses, ties, scored, against, played;
public Main(String x){
this.name = x;
this.points = 0;
this.wins = 0;
this.losses = 0;
this.ties = 0;
this.scored = 0;
this.against = 0;
this.played = 0;
}
public int compareTo(Main x){
if(this.points != x.points)
return x.points-this.points;
if(this.wins != x.wins)
return x.wins-this.wins;
if((this.scored-this.against) != (x.scored-x.against))
return ((x.scored-x.against) - (this.scored-this.against) );
if(this.scored != x.scored)
return x.scored-this.scored;
if(this.played != x.played)
return this.played-x.played;
return this.stringCompare(x.name);
}
public int stringCompare(String x){
for(int a = 0;a < Math.min(this.name.length(), x.length());a++){
if(Character.isLetter(this.name.charAt(a)) && Character.isLetter(x.charAt(a))){
if(Character.toUpperCase(this.name.charAt(a)) != Character.toUpperCase(x.charAt(a)))
return Character.toUpperCase(this.name.charAt(a))-Character.toUpperCase(x.charAt(a));
}
else{
if(this.name.charAt(a) != x.charAt(a))
return this.name.charAt(a)-x.charAt(a);
}
}
return this.name.length()-x.length();
}
public static void main(String[] args)throws Exception{
Scanner In = new Scanner(System.in);
TreeMap<String, Integer> mapping;
int numGames = Integer.parseInt(In.nextLine());
for(int currGame = 0;currGame < numGames;currGame++){
String tournament = In.nextLine();
int numTeams = Integer.parseInt(In.nextLine());
mapping = new TreeMap<String, Integer>();
Main teams[] = new Main[numTeams];
for(int currTeam = 0;currTeam < numTeams;currTeam++){
String teamName = In.nextLine();
teams[currTeam] = new Main(teamName);
mapping.put(teamName, new Integer(currTeam));
}
int numMatches = Integer.parseInt(In.nextLine());
for(int currMatch = 0;currMatch < numMatches;currMatch++){
String game = In.nextLine();
String teamOne = game.substring(0, game.indexOf("#"));
game = game.substring(game.indexOf("#")+1);
int scoreOne = Integer.parseInt(game.substring(0, game.indexOf("@")));
game = game.substring(game.indexOf("@")+1);
int scoreTwo = Integer.parseInt(game.substring(0, game.indexOf("#")));
game = game.substring(game.indexOf("#")+1);
String teamTwo = game;
teams[mapping.get(teamOne).intValue()].scored+= scoreOne;
teams[mapping.get(teamOne).intValue()].against+= scoreTwo;
teams[mapping.get(teamOne).intValue()].played++;
teams[mapping.get(teamTwo).intValue()].scored+= scoreTwo;
teams[mapping.get(teamTwo).intValue()].against+= scoreOne;
teams[mapping.get(teamTwo).intValue()].played++;
if(scoreOne > scoreTwo){
teams[mapping.get(teamOne).intValue()].wins++;
teams[mapping.get(teamOne).intValue()].points+= 3;
teams[mapping.get(teamTwo).intValue()].losses++;
}
else if(scoreTwo > scoreOne){
teams[mapping.get(teamTwo).intValue()].wins++;
teams[mapping.get(teamTwo).intValue()].points+= 3;
teams[mapping.get(teamOne).intValue()].losses++;
}
else{
teams[mapping.get(teamOne).intValue()].ties++;
teams[mapping.get(teamOne).intValue()].points++;
teams[mapping.get(teamTwo).intValue()].ties++;
teams[mapping.get(teamTwo).intValue()].points++;
}
}
Arrays.sort(teams);
if(currGame!=0)
System.out.println();
System.out.println(tournament);
for(int currTeam = 0;currTeam < numTeams;currTeam++){
System.out.println((currTeam+1)+") "+teams[currTeam].name+" "+teams[currTeam].points+"p, "+
teams[currTeam].played+"g ("+teams[currTeam].wins+"-"+teams[currTeam].ties+"-"+teams[currTeam].losses+"), "+
(teams[currTeam].scored-teams[currTeam].against)+"gd ("+teams[currTeam].scored+"-"+teams[currTeam].against+")");
}
}
}
}
If my problem was truly the difference in cases and the characters between the two cases, I created this new function
- Code: Select all
public int stringCompare(String x){
for(int a = 0;a < Math.min(this.name.length(), x.length());a++){
if(Character.isLetter(this.name.charAt(a)) && Character.isLetter(x.charAt(a))){
if(Character.toUpperCase(this.name.charAt(a)) != Character.toUpperCase(x.charAt(a)))
return Character.toUpperCase(this.name.charAt(a))-Character.toUpperCase(x.charAt(a));
}
else{
if(this.name.charAt(a) != x.charAt(a))
return this.name.charAt(a)-x.charAt(a);
}
}
return this.name.length()-x.length();
}
It goes through each character and checks if they're both letters. If so, changes them both to uppercase and compares. If not, compares them as normal. If they are lexicographically equal up to the length of the shortest string, it gives precedence for the shorter of the 2. So what this means is that a > [ > A == a. But still, a WA. Can't think of what else could be wrong.
