|
|
back to boardI get wa 7, please give me test 7 Posted by JAVATAR 18 Apr 2012 01:10 Hello, What is on test 7?I get wa7 but tried all cases.please help. here is my program. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; /** * * @author Imran */ public class IsenbaevsNumber {
public static void main(String args[]) throws IOException{
Map m1=new HashMap(); Map m2=new HashMap(); List<Group> participantGroups=new ArrayList<Group>();;
InputStreamReader isr=new InputStreamReader(System.in); BufferedReader bfr=new BufferedReader(isr);
int n=Integer.parseInt(bfr.readLine()); int i=0; StringBuilder sb; int id=0;
while(i<n){ i++; //read the string sb=new StringBuilder(bfr.readLine());
//get first participant String p1=sb.substring(0, sb.indexOf(" ")); sb.delete(0,sb.indexOf(" ")+1); //get second participant String p2=sb.substring(0, sb.indexOf(" ")); sb.delete(0,sb.indexOf(" ")+1); //get third partisipant String p3=sb.toString();
Participant po1=(Participant)m1.get(p1); int mm=4;
if(p1.equals("Isenbaev")||p2.equals("Isenbaev")||p3.equals("Isenbaev")) mm=2;
if(po1==null){ if(p1.equals("Isenbaev")) po1=new Participant(p1, 0,id); else po1=new Participant(p1, mm,id); m1.put(p1, po1); m2.put(po1.id, po1); id++; }
Participant po2=(Participant)m1.get(p2); if(po2==null){ if(p2.equals("Isenbaev")) po2=new Participant(p2, 0,id); else po2=new Participant(p2, mm,id); m1.put(p2, po2); m2.put(po2.id, po2); id++; }
Participant po3=(Participant)m1.get(p3); if(po3==null){ if(p3.equals("Isenbaev")) po3=new Participant(p3, 0,id); else po3=new Participant(p3, mm,id); m1.put(p3, po3); m2.put(po3.id, po3); id++; }
Group g=new Group(); g.p1id=po1.id; g.p2id=po2.id; g.p3id=po3.id;
participantGroups.add(g); }
int[][] graph=new int[id][id]; for(int j=0;j<participantGroups.size();j++){
graph[participantGroups.get(j).p1id][participantGroups.get(j).p2id]=1; graph[participantGroups.get(j).p2id][participantGroups.get(j).p3id]=1; graph[participantGroups.get(j).p1id][participantGroups.get(j).p3id]=1;
graph[participantGroups.get(j).p2id][participantGroups.get(j).p1id]=1; graph[participantGroups.get(j).p3id][participantGroups.get(j).p2id]=1; graph[participantGroups.get(j).p3id][participantGroups.get(j).p1id]=1; }
for(int j=0;j<id;j++){ Participant pt=(Participant)m2.get(j); for(int k=0;k<id;k++){ Participant pt2=(Participant)m2.get(k); if(graph[j][k]==1&&pt2.value<pt.value) pt.value=pt2.value+1; }
}
List<Participant> participantsByName = new ArrayList<Participant>(m1.values()); Collections.sort(participantsByName,new NameComparator());
for( Participant par : participantsByName){ if(par.value==4) System.out.println(par.name+" undefined"); else System.out.println(par.name+" "+par.value); } } } class Participant{ int value=4; String name; int id;
Participant(String name,int value,int id){ this.name=name; this.value=value; this.id=id; }
@Override public String toString(){ return this.name; }
public void setValue(int value){ this.value=value; }
} //Comparator class NameComparator implements Comparator{ @Override public int compare(Object o1, Object o2)throws UnsupportedOperationException { Participant p1=(Participant)o1; Participant p2=(Participant)o2;
if(p1.name.compareTo(p2.name)>0) return 1; else return 0;
} } class Group{ int p1id; int p2id; int p3id; }
Re: I get wa 7, please give me test 7 Ошибка в сортировке по алфавитному порядку !! тест не знаю, но ошибка точно в сортировке Re: I get wa 7, please give me test 7 Thank you very much! I did not pay attention to sorting will try to fix and see. Re: I get wa 7, please give me test 7 Re: I get wa 7, please give me test 7 I have used something like: Person[] data = new Person[1000]; class Person implements Comparable{ // some code public int compareTo(Object o){ // here you write when to return <, =, > } } after that: Arrays.sort(data); or if you have Collection: Collections.sort(data); Edited by author 07.05.2012 16:58 |
|
|