/ ** * 13 balls Java implementation *
Copyright: Copyright (c) 2004 p> * @Author Treerot * @version 1.0 * / public class thirteenball {private static class ball {private int future; public; public; public; INT getWeight () {Return Weight;
Public void setWeight (int weight) {this.weight = weight;
Ball (int weight) {this.weight = weight;}}
Public static void main (string [] args) throws interruptedException {ball [] balls = new ball [13]; / * for (int i = 0; i <13; i ) {for (int J = 0; J <13 ; J ) {balls [j] = new ball (10);} // set a light ball Balls [i] .SetWeight (8); system.out.println (Findball (Balls)); // Set a heavy ball Balls [i] .SetWeight (12); System.out.Println (Findball (Balls));} * /}
Static int compare (ball [] b, int [] b1, int [] b2) {int LEFT = 0, Right = 0; for (int i = 0; i Static void print (int flag) {string s = (FLAG == 0)? "Unable to judge the weight!": (Flag <0)? "Target Ball is better than other light": "Target ball than other heavy"; system.out .println (s); Static int Findball (Ball [] B) {// 天 平 四 四 四:: B [0], B [1], B [2], B [3] VS B [4], B [5], B [6], B [7] // Target Ball Int Result = 0; INT CMP1 = 0, CMP2 = 0, CMP3 = 0; CMP1 = Compare (B, New Int "{0, 1, 2, 3}, new int [] {4, 5, 6, 7}); IF (CMP1 == 0) {// Target Ball in 5 in the back: eight balls in front, compare 8, 9 VS 10, 0 (or other 0..7) CMP2 = COMPARE (B, New Int [] {8, 9}, new int [] {10, 0}); if (CMP2 == 0) {// Target ball in 11,12: Compare 11 VS 0 CMP3 = Compare (B, New Int " ] {11}, new int [] {0}); if (cmp3 == 0) {// Target ball is 12 results = 12; print (0); // This is unable to determine the light weight} else {// Target Ball Is 11 results = 11; Print (CMP3);}} else IF (cmp2 <0) {// 8, 9 light or 10 heavy, compare 8 vs 9 cmp3 = compare (b, new int in) {8}, New INT [] {9}); if (cmp3 == 0) {// 10 heavy result = 10; print (1);} else if (cmp3 <0) {// 8 light Result = 8; print (-1 );} Else {// 9 light result = 9; print (-1);}} else {// 8, 9 heavy or 10 light CMP3 = Compare (b, new int in) {8}, new int in {9}); if (CMP3 == 0) {// 10 light result = 10; print (-1);} else if (cmp3 <0) {// 9 heavy result = 9 PRINT (1);} else {// 8 Result = 8; Print (1);} }}} Else IF (CMP1 <0) {// 0, 1, 2, 3 Light or 4, 5, 6,7th Compare: 0, 1, 4 VS 2, 3, 5 CMP2 = COMPARE (B, New Int [] {0, 1, 4}, new int in {2, 3, 5}); if (cmp2 == 0) {// 6, 7 heavy, compare 6 vs 0 CMP3 = Compare (b, new int int INT [] {6}, new int [] {0}); // CMP3 will not be less than 0 IF (CMP3 == 0) {// 7 heavy Result = 7; print (1);} else {// 6 Result = 6; Print (1);}} else IF (CMP2 <0) {// 0, 1 light or 5 weight comparison 0 vs 1 cmp3 = compare (b, new int in] {0}, new int] {1}); if (CMP3 == 0) {// 5 heavy result = 5; print (1);} else if (cmp3 <0) {// 0 light result = 0; print (-1);} Else {// 1 Light Result = 1; Print (-1);}} else {// 2, 3 Light or 4 Compare 2 VS 3 CMP3 = Compare (B, New Int Int "{2}, new int 网] {3}); if (CMP3 == 0) {// 4 heavy result = 4; Print (1);} else IF (CMP3 <0) {// 2 Light Result = 2; print (-1);} Else {// 3 Light Result = 3; print (-1);}}}}}}}}}}}}} else {// and above symmetry 0, 1, 2, 3 weight or 4, 5, 6, 7 lighter comparison: 0, 1, 4 vs 2, 3, 5 // Realized is not willing to write, here is recursive, it is not allowed to compare three times. It is really for lazy ball [] b2 = new ball [13]; system.Arraycopy (B, 4, b2 , 0, 4); System.ArrayCopy (B, 0, B2, 4, 4); Result = Findball (B2) -1; if (Result> = 4) Result- = 4; Else Result = 4;} Return Result 1; }