Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class PlayerRanking {
- public static void main(String[] args) throws Exception {
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- String[][] player=new String[100000][5];
- String[][] findArray=new String[7][2];
- String[] sorted=new String[100000];
- int count=0;
- int index=0;
- while(true){
- String[] tokens = in.readLine().split(" ");
- if(tokens[0].equals("add")){
- player[index][0]=tokens[1];//PLAYER_NAME
- player[index][1]=tokens[2] ;//PLAYER_TYPE
- player[index][2]=tokens[3] ;//PLAYER_AGE
- if(count==0){
- player[index][3]=tokens[4];//PAYER_POSITION
- System.out.println("Added player "+tokens[1]+" to position "+ tokens[4]);
- count++;
- index++;
- }else{
- //player[index][3]=
- if(isPosAvailable(player,(Integer.parseInt(tokens[4])),count)){
- player=(UpdateAndAdd(player,Integer.parseInt(tokens[4]),count,index)).clone();
- //System.out.println("Its available");
- System.out.println("Added player "+tokens[1]+" to position "+ tokens[4]);
- count++;
- index++;
- }else{
- //System.out.println("Its not available");
- player=(UpdateAndAdd(player,Integer.parseInt(tokens[4]),count,index)).clone();
- System.out.println("Added player "+tokens[1]+" to position "+ tokens[4]);
- count++;
- index++;
- }
- //PAYER_POSITION
- }
- }
- if(tokens[0].equals("find")){
- for(int i=0;i<count;i++){
- System.out.println(getName(player,i)+" "+getType(player,i)+" "+getAge(player,i)+" "+getPos(player,i));
- }
- findArray=sort(player,count,index);
- for(int i=0;i<count;i++){
- System.out.println(getName(findArray,i)+" "+getType(findArray,i)+" "+getAge(findArray,i)+" "+getPos(findArray,i));
- }
- System.out.println(getIndexPos(player,"1",count));
- }
- if(tokens[0].equals("ranklist")){
- int ranklistA=Integer.parseInt(tokens[1]);
- int ranklistB=Integer.parseInt(tokens[2]);
- for(int i=(ranklistA-1);i<(ranklistB-1);i++){
- System.out.println(player[i][3]);
- }
- }
- if(tokens[0].equals("end")){
- break;
- }
- }
- }
- public static boolean isPosAvailable(String [][] rankaddarray,int pos,int count){
- boolean isAvbl=false;
- for(int i=0;i<count;i++){
- if((rankaddarray[i][3]).equals(String.valueOf(pos))){
- isAvbl=false;
- break;
- }else{
- isAvbl=true;
- }
- }
- return isAvbl;
- }
- public static String [][] UpdateAndAdd(String [][] rankaddarray,int pos,int count, int index){
- int a = 1;
- for(int i=0;i<count;i++){
- if((Integer.parseInt(rankaddarray[i][3]))>=pos){
- rankaddarray[i][3]=String.valueOf((Integer.parseInt(rankaddarray[i][3]))+ a);
- }else{continue;}
- }
- rankaddarray[index][3]=String.valueOf(pos);
- return rankaddarray;
- }
- public static String [][] sort(String [][] data,int count, int index){
- for(int k=1;k<count;k++){
- String[][] current=data[k][*];
- int j=k;
- while(j>0 && data[j-1]>current){
- data[j]=data[j-1];
- j--;
- }
- data[j]=current;
- }
- return data;
- }
- public static String getName(String[][] data,int instance){
- String pos=data[instance][0];
- return pos;
- }
- public static String getType(String[][] data,int instance){
- String pos=data[instance][1];
- return pos;
- }
- public static String getAge(String[][] data,int instance){
- String pos=data[instance][2];
- return pos;
- }
- public static String getPos(String[][] data,int instance){
- String pos=data[instance][3];
- return pos;
- }
- public static int getIndexPos(String[][] data,String pos, int count){
- int index=0;
- for(int i=0;i<count;i++){
- if((data[i][3]).equals(pos)){
- index=i;
- break;
- }
- }
- return index;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment