成绩排名——华为上机笔试题

成绩排名——华为上机笔试题_第1张图片




import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
	
	public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        ArrayList<Person> list = new ArrayList<Person>();
    	ArrayList<Integer> mathlevel = new ArrayList<Integer>();
    	ArrayList<Integer> langlevel = new ArrayList<Integer>();
    	ArrayList<Integer> totallevel = new ArrayList<Integer>();
        while(sc.hasNext()){
        	String cmd = sc.nextLine();
        	String[] fen = cmd.split(":|,|;|=");
        	String name="";
        	int mathgrade=0,langgrade=0;
        	boolean flag = true;
        	for(int i = 0; i<fen.length;i++){
        		if(fen[i].equals("LST GRADE")){
        			flag = false;
        		}
        		if(fen[i].equals("NAME")){
        			name = fen[i+1];
        			i++;
        		}
        		if(fen[i].equals("MATH")){
        			mathgrade = Integer.valueOf(fen[i+1]);
        			i++;
        		}
        		if(fen[i].equals("LANG")){
        			langgrade = Integer.valueOf(fen[i+1]);
        			i++;
        		}
        	}
        	if(flag == true){
        		Person person = new Person(name,mathgrade,langgrade);
        		list.add(person);
        		mathlevel.add(mathgrade);
        		langlevel.add(langgrade);
        		totallevel.add(mathgrade+langgrade);
        		
        	}
        	else{
        		int fmath=0;
        		int flang=0;
        		int ftotal=0;
        		String fname="";
        		for(Person person:list){
        			if(person.name.equals(name)){
        				fname = person.name;
        				fmath =person.mathgrade;
        				flang = person.langgrade;
        				ftotal = fmath+flang;
        				break;
        			}
        		}
        		int mathcount=level(fmath,mathlevel);
        		int langcount=level(flang,langlevel);
        		int totalcount=level(ftotal,totallevel);
        		mathlevel.clear();
        		langlevel.clear();
        		totallevel.clear();
        		list.clear();
        		System.out.println(fname+" "+fmath+" "+flang+" "+ftotal+" "+mathcount+" "+langcount+" "+totalcount);
        		
        	}       	
        }
                        
    }
	
	@SuppressWarnings("unchecked")
	static int level(int grade,ArrayList<Integer> list){
		int count = 0;
		Collections.sort(list,new SortbyInt());
		for(int i:list){
			count++;
			if(grade == i){
				break;
			}
		}
		return count;
	}
    
}

class Person{
	String name;
	int mathgrade;
	int langgrade;
	public Person(String name, int mathgrade, int langgrade) {		
		this.name = name;
		this.mathgrade = mathgrade;
		this.langgrade = langgrade;
	}
	
}
class SortbyInt implements Comparator{

	@Override
	public int compare(Object o1, Object o2) {
		// TODO Auto-generated method stub
		int i1 =(int)o1;
		int i2 =(int)o2;
		if(i1>i2){
			return -1;
		}
		return 1;
		
	}
	
}





你可能感兴趣的:(成绩排名——华为上机笔试题)