Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.06 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7.  
  8. public class Hakbat_Sul {
  9. static int Players=5,Rank=TheRank(Players),Games=TheGames(Players),t=0,row,column,numofgames;
  10. static double firstodd,secondodd,Total;
  11. static double TheMainBoard[][]=new double [Rank][Games+1];
  12. static double TheMultyBoard[][]=new double [Rank][Games];
  13. static double TheFinalBoard[]=new double [Games+1];
  14. static String TheGameBoard[]=new String [Games];
  15. static String TheRankBoard[][]=new String [Rank][Players];
  16. static int ThePlayerboard[]= new int[Players];
  17. static int TheRankingBoard[][]= new int [Players][Players];
  18. static double TheODDBoard[][]= new double [Rank][Games];
  19. static String TheFirstRank[]=new String [Players];
  20.  
  21.  
  22.  
  23.  
  24. public static void main(String[] args) {
  25. readFromFile();
  26.  
  27. row=0;
  28. column=0;
  29. Total=0;
  30. FillPlayers();
  31. ranks();
  32. MakeGameBoard();
  33. MakeRank();
  34. MakingTheOddTable();
  35. TheLastTable();
  36. Solve();
  37. System.out.print(TheFinalBoard[Players]);
  38. }
  39.  
  40.  
  41. private static void readFromFile(){
  42. try(BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
  43. StringBuilder sb = new StringBuilder();
  44. String line = br.readLine();
  45. List<String> Result=new LinkedList<String>(); //intialize List of Strings
  46. Result.add(line); //add First Line (String) to the List
  47. while (line != null) {
  48. sb.append(line);
  49. sb.append(System.lineSeparator());
  50. line = br.readLine();
  51. Result.add(line);
  52. }
  53. String[] array = Result.toArray(new String[Result.size()]);
  54. setValuesFrom(array);
  55. }
  56. catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. private static void setValuesFrom(String[] File){
  61.  
  62. for (int i=0;i<File.length;i++){
  63. switch (File[i].charAt(0)){
  64. case 'n':
  65. String re=File[i].substring(2);
  66. Players=Integer.valueOf(re);
  67. break;
  68.  
  69. case 'k':
  70. String res=File[i].substring(2);
  71. numofgames=Integer.valueOf(res);
  72. break;
  73.  
  74. case 'R':
  75. i=i+2;
  76. for (int j=0;j<Players;j++){ //Adding Ranking - index 0 is first place
  77. TheFirstRank[j]=File[i];
  78. i=i+1;
  79. }
  80. case 'B':
  81. i=i+1;
  82. int c=Players-1;
  83. while (c>=0){
  84. if (!Character.isDigit(File[i].charAt(0)))
  85. i++;
  86. else {
  87. for (int r=0;r<TheRankingBoard.length;r++){
  88. TheRankingBoard[r][c]=Integer.valueOf(File[i]); // transfering string to int
  89. i=i+1;
  90. }
  91. c--; // going one column to the left
  92. }
  93. }
  94. break;
  95. }
  96. }
  97. }
  98. private static void FillPlayers(){
  99. for (int i=0;i<Players;i++)
  100. ThePlayerboard[i]=i+1;
  101.  
  102. }
  103. public static void TheLastTable(){
  104. double max =0;
  105. for(row=0;row<Rank;row++){
  106. for(column=0;column<Games;column++){
  107. if(TheMainBoard[row][column]>max) max = TheMainBoard[row][column];
  108.  
  109. }
  110. TheMainBoard[row][Players]=max;
  111. max=0;
  112. }
  113.  
  114. }
  115.  
  116.  
  117.  
  118.  
  119. public static void Solve(){
  120. numofgames=numofgames-1;
  121. while(numofgames!=1 ){
  122. for (row=0;row<Rank;row++){
  123. for (column=0;column<Games;column++){
  124. Total=TheMultyBoard[row][column];
  125. Total=Total+(TheODDBoard[row][column]*Win(TheRankBoard[row],TheGameBoard[column]))+((1-TheODDBoard[row][column])*Lost(TheRankBoard[row],TheGameBoard[column]));
  126. TheMainBoard[row][column]=Total;
  127. }
  128.  
  129. }
  130. TheLastTable();
  131. numofgames=numofgames-1;
  132. }
  133. if(numofgames==1){
  134. int count =0;
  135. int rows =0;
  136.  
  137. for(int j=0;j<Rank;j++){
  138. for(int k=0;k<Players;k++){
  139. if(TheFirstRank[k].equals(TheRankBoard[j][k])) count++;
  140. if(count==Players) rows=j;
  141. if(rows!=0) break;
  142. }
  143. if(rows!=0) break;
  144. count=0;
  145. }
  146.  
  147.  
  148.  
  149.  
  150. for (column=0;column<Games;column++){
  151. Total=TheMultyBoard[rows][column];
  152. Total=Total+(TheODDBoard[rows][column]*Win(TheRankBoard[rows],TheGameBoard[column]))+((1-TheODDBoard[rows][column])*Lost(TheRankBoard[rows],TheGameBoard[column]));
  153. TheFinalBoard[column]=Total;
  154. }
  155.  
  156.  
  157. TheFinal();
  158.  
  159.  
  160. }
  161. }
  162. private static void TheFinal() {
  163.  
  164. double max =0;
  165. for(int i=0;i<Games;i++){
  166. if(TheFinalBoard[i]>max) max = TheFinalBoard[i];
  167.  
  168. }
  169. TheFinalBoard[Players]=max;
  170. max=0;
  171. }
  172. public static double Win(String X[],String Y) {
  173. int count =0,rows=0;
  174. String Z[]=new String[X.length];
  175.  
  176. for(int l=0;l<X.length;l++){
  177. Z[l]=X[l];
  178. }
  179.  
  180. char winner = Y.charAt(0);
  181. char looser = Y.charAt(1);
  182. String temp;
  183. int i=0;
  184. int winnerplace=0,looserplace=0;
  185. while (i<Players){
  186. if(winner==Z[i].charAt(0)) winnerplace=i;
  187. if(looser==Z[i].charAt(0)) looserplace=i;
  188. i++;
  189. }
  190.  
  191.  
  192. for(int j=0;j<Rank;j++){
  193. for(int k=0;k<Players;k++){
  194. if(Z[k].equals(TheRankBoard[j][k])) count++;
  195. if(count==Players) rows=j;
  196. if(rows!=0) break;
  197. }
  198. if(rows!=0) break;
  199. count=0;
  200. }
  201.  
  202. if(winnerplace<looserplace) return TheMainBoard[rows][Players];
  203. else{
  204. temp =Z[winnerplace];
  205. Z[winnerplace]=Z[looserplace];
  206. Z[looserplace]=temp;
  207. if(TheRankBoard[0][0]!=null);
  208. count=0;
  209. rows=0;
  210. for(int j=0;j<Rank;j++){
  211. for(int k=0;k<Players;k++){
  212. if(Z[k].equals(TheRankBoard[j][k])) count++;
  213. if(count==Players) rows=j;
  214. if(rows!=0) break;
  215. }
  216. if(rows!=0) break;
  217. count=0;
  218. }
  219. double x=TheMainBoard[rows][Players];
  220. return x;
  221.  
  222.  
  223.  
  224. }
  225.  
  226. }
  227. public static double Lost(String[] X,String Y) {
  228.  
  229. String Z[]=new String[X.length];
  230.  
  231. for(int l=0;l<X.length;l++){
  232. Z[l]=X[l];
  233. }
  234.  
  235. char winner = Y.charAt(1);
  236. char looser = Y.charAt(0);
  237. String temp;
  238. int i=0,rows=0,count=0;
  239. int winnerplace=0,looserplace=0;
  240. while (i<Players){
  241. if(winner==Z[i].charAt(0)) winnerplace=i;
  242. if(looser==Z[i].charAt(0)) looserplace=i;
  243. i++;
  244. }
  245.  
  246. for(int j=0;j<Rank;j++){
  247. for(int k=0;k<Players;k++){
  248. if(Z[k].equals(TheRankBoard[j][k])) count++;
  249. if(count==Players) rows=j;
  250. if(rows!=0) break;
  251. }
  252. if(rows!=0) break;
  253. count=0;
  254. }
  255.  
  256.  
  257.  
  258.  
  259. if(winnerplace<looserplace) return TheMainBoard[rows][Players];
  260. else{
  261. temp =Z[winnerplace];
  262. Z[winnerplace]=Z[looserplace];
  263. Z[looserplace]=temp;
  264. if(TheRankBoard[0][0]!=null);
  265. count=0;
  266. rows=0;
  267. for(int j=0;j<Rank;j++){
  268. for(int k=0;k<Players;k++){
  269. if(Z[k].equals(TheRankBoard[j][k])) count++;
  270. if(count==Players) rows=j;
  271. if(rows!=0) break;
  272. }
  273. if(rows!=0) break;
  274. count=0;
  275. }
  276.  
  277. return TheMainBoard[rows][Players];
  278.  
  279.  
  280.  
  281. }
  282.  
  283.  
  284.  
  285. }
  286.  
  287.  
  288.  
  289.  
  290. public static int TheRank(int n){//עצרת כדי למצוא את גודל המערך
  291. if (n==0 || n==1)
  292. return 1;
  293.  
  294. return n*TheRank(n-1);}
  295. public static int TheGames(int Games1){// כדי למצוא את גודל מערך של המשחקים
  296. int Games2=(TheRank(Games1)/(TheRank(Games1-2)*TheRank(2)));
  297.  
  298. return Games2;
  299. }
  300. public static void ranks(){
  301. for (int i=0;i<Rank;i++)
  302. for (int j=0;j<Players;j++) TheRankBoard[i][j]="";
  303.  
  304.  
  305. }
  306. public static void MakeGameBoard(){
  307.  
  308. for (int i=1;i<Players;i++){
  309. ThePlayerboard[i-1]=i;
  310. }
  311. int i=0;
  312.  
  313.  
  314. for(int Firstplayer=1;Firstplayer<=Players;Firstplayer++){
  315. for (int second=2;second<=Players;second++){
  316. if (Firstplayer<second){
  317. String game=""+Firstplayer+""+second;
  318. TheGameBoard[i]=game;
  319. i++;
  320.  
  321. }
  322. }
  323. }
  324.  
  325.  
  326. }
  327.  
  328.  
  329.  
  330.  
  331. public static void MakeRank(){
  332.  
  333. Hakbat_Sul p=new Hakbat_Sul();
  334. p.permute(ThePlayerboard, 0);}
  335. public void printArray(int []a) {
  336. for (int i = 0; i<a.length; i++) {
  337. TheRankBoard[t][i]=""+a[i];
  338. }
  339.  
  340. t++;
  341. }
  342. public void permute(int []a,int k ) {
  343. if(k==a.length)
  344. printArray(a);
  345. else
  346. for (int i = k; i <a.length; i++) {
  347. int temp=a[k];
  348. a[k]=a[i];
  349. a[i]=temp;
  350. permute(a,k+1);
  351. temp=a[k];
  352. a[k]=a[i];
  353. a[i]=temp;}
  354.  
  355. }
  356.  
  357.  
  358.  
  359.  
  360. public static void MakingTheOddTable(){
  361. for (int i=0;i<Rank;i++){
  362. for (int j=0;j<Games;j++){
  363.  
  364. char first=TheFirst(TheGameBoard[j]);
  365. int FirstRank =CheckWhatPlace(first,i);
  366.  
  367. char second=TheSecond(TheGameBoard[j]);
  368. int SecondRank =CheckWhatPlace(second,i);
  369.  
  370. double odd=TheOdd(first,FirstRank,second,SecondRank);
  371. TheMultyBoard[i][j]=(int) (firstodd*secondodd);
  372. TheMainBoard [i][j]=(int) (firstodd*secondodd);
  373. TheODDBoard[i][j]=odd;
  374. }
  375. }
  376.  
  377.  
  378. }
  379. public static double TheOdd(char firstplace ,int FirstRating ,char secondplace, int SecondRating){
  380.  
  381. int first=(int)firstplace-48;
  382. int second=(int)secondplace-48;
  383.  
  384.  
  385. firstodd=TheRankingBoard[FirstRating-1][first-1];
  386. secondodd=TheRankingBoard[SecondRating-1][second-1];
  387.  
  388. double thesum=firstodd-secondodd;
  389.  
  390. if(thesum<0){
  391. thesum=thesum*-1;
  392. double TheODD=1/(2+(thesum));
  393. return TheODD;
  394. }
  395.  
  396. double TheODD=1/(2+(thesum));
  397. return 1-TheODD;
  398. }
  399. static int CheckWhatPlace(char f,int row) {
  400. int place=0;
  401. int i=0;
  402. for (i=0;i<Players;i++){
  403. if(f==TheRankBoard[row][i].charAt(0)) place=i+1;
  404.  
  405. }
  406. return place;
  407. }
  408. public static char TheFirst(String game){
  409. char Thefirst=game.charAt(0);
  410. return Thefirst;
  411. }
  412. public static char TheSecond(String game){
  413. char TheSec=game.charAt(1);
  414. return TheSec;
  415. }
  416.  
  417.  
  418. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement