Guest User

Untitled

a guest
Feb 18th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class PlayerRanking {
  5. public static void main(String[] args) throws Exception {
  6. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  7. String[][] player=new String[100000][5];
  8. String[][] findArray=new String[7][2];
  9. String[] sorted=new String[100000];
  10. int count=0;
  11. int index=0;
  12. while(true){
  13. String[] tokens = in.readLine().split(" ");
  14. if(tokens[0].equals("add")){
  15. player[index][0]=tokens[1];//PLAYER_NAME
  16. player[index][1]=tokens[2] ;//PLAYER_TYPE
  17. player[index][2]=tokens[3] ;//PLAYER_AGE
  18. if(count==0){
  19. player[index][3]=tokens[4];//PAYER_POSITION
  20. System.out.println("Added player "+tokens[1]+" to position "+ tokens[4]);
  21. count++;
  22. index++;
  23. }else{
  24. //player[index][3]=
  25. if(isPosAvailable(player,(Integer.parseInt(tokens[4])),count)){
  26. player=(UpdateAndAdd(player,Integer.parseInt(tokens[4]),count,index)).clone();
  27. //System.out.println("Its available");
  28. System.out.println("Added player "+tokens[1]+" to position "+ tokens[4]);
  29. count++;
  30. index++;
  31. }else{
  32. //System.out.println("Its not available");
  33. player=(UpdateAndAdd(player,Integer.parseInt(tokens[4]),count,index)).clone();
  34. System.out.println("Added player "+tokens[1]+" to position "+ tokens[4]);
  35. count++;
  36. index++;
  37. }
  38. //PAYER_POSITION
  39. }
  40. }
  41. if(tokens[0].equals("find")){
  42. for(int i=0;i<count;i++){
  43. System.out.println(getName(player,i)+" "+getType(player,i)+" "+getAge(player,i)+" "+getPos(player,i));
  44. }
  45. findArray=sort(player,count,index);
  46. for(int i=0;i<count;i++){
  47. System.out.println(getName(findArray,i)+" "+getType(findArray,i)+" "+getAge(findArray,i)+" "+getPos(findArray,i));
  48. }
  49. System.out.println(getIndexPos(player,"1",count));
  50.  
  51. }
  52. if(tokens[0].equals("ranklist")){
  53. int ranklistA=Integer.parseInt(tokens[1]);
  54. int ranklistB=Integer.parseInt(tokens[2]);
  55. for(int i=(ranklistA-1);i<(ranklistB-1);i++){
  56. System.out.println(player[i][3]);
  57. }
  58. }
  59. if(tokens[0].equals("end")){
  60. break;
  61. }
  62. }
  63. }
  64. public static boolean isPosAvailable(String [][] rankaddarray,int pos,int count){
  65. boolean isAvbl=false;
  66. for(int i=0;i<count;i++){
  67. if((rankaddarray[i][3]).equals(String.valueOf(pos))){
  68. isAvbl=false;
  69. break;
  70. }else{
  71. isAvbl=true;
  72. }
  73. }
  74. return isAvbl;
  75. }
  76. public static String [][] UpdateAndAdd(String [][] rankaddarray,int pos,int count, int index){
  77. int a = 1;
  78. for(int i=0;i<count;i++){
  79. if((Integer.parseInt(rankaddarray[i][3]))>=pos){
  80. rankaddarray[i][3]=String.valueOf((Integer.parseInt(rankaddarray[i][3]))+ a);
  81. }else{continue;}
  82. }
  83. rankaddarray[index][3]=String.valueOf(pos);
  84. return rankaddarray;
  85. }
  86. public static String [][] sort(String [][] data,int count, int index){
  87. for(int k=1;k<count;k++){
  88. String[][] current=data[k][*];
  89. int j=k;
  90. while(j>0 && data[j-1]>current){
  91. data[j]=data[j-1];
  92. j--;
  93. }
  94. data[j]=current;
  95. }
  96. return data;
  97. }
  98.  
  99.  
  100. public static String getName(String[][] data,int instance){
  101. String pos=data[instance][0];
  102. return pos;
  103. }
  104. public static String getType(String[][] data,int instance){
  105. String pos=data[instance][1];
  106. return pos;
  107. }
  108. public static String getAge(String[][] data,int instance){
  109. String pos=data[instance][2];
  110. return pos;
  111. }
  112. public static String getPos(String[][] data,int instance){
  113. String pos=data[instance][3];
  114. return pos;
  115. }
  116. public static int getIndexPos(String[][] data,String pos, int count){
  117. int index=0;
  118. for(int i=0;i<count;i++){
  119. if((data[i][3]).equals(pos)){
  120. index=i;
  121. break;
  122. }
  123. }
  124. return index;
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment