One balance and 12 balls

Source: Internet
Author: User

There are 12 balls, one of which has a weight different from the other 11 balls. The weights of the other 11 balls are the same. The condition is that there is only one balance. The balance must be weighed three times and the ball is retrieved.

If (ABCD = 1234) {// The First Time
// The target is in ABCD.
If (ABC = ABC) {// ABC is the second standard ball...
// Target d
If (D> 1) {// the third time
Bad ball is D, heavy;
} Else {
Bad ball is D, light;
}
} Elseif (ABC> ABC) {// ABC is the second standard ball...
// The target is in ABC and the Bad ball is heavy.
If (AA = bb) {// The third time ......
The Bad ball is C and heavy;
} Elseif (AA> BB) {// The third time ......
Bad ball is a, heavy;
} Else {// The third time ......
Bad ball is B, heavy
}
} Else {// ABC is the second standard ball...
// The target is in ABC and the Bad ball is heavy.
// Same as the preceding solution, the third time ......
}
} Elseif (ABCD> 1234) {// for the first time... then ABCD is standard
// The target is in abcd1234 and 234 is removed.
If (AABC = 1bcd) {// the second time...
// The target is in 234... light ..
// The third time... the solution is the same as above...
} Elseif (AABC <1bcd ){
// The target is in BCD...
// The third time... the solution is the same as above...
} Else {
// In the target a or 1... 1 is light, and a is heavy.
}
} Else {// The first time... ABCD <1234
// The target is in abcd1234 and the BCD is removed.
If (a234> 1abc ){//.....
// The target value is 234 ..
} Elseif (a234 <1abc ){
// When the target is a or 1... A, it is light... 1, it is heavy ..
} Else {
// Target in BCD... light ..
// The third time... the solution is the same as above...
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.