C ++ Example 8

Source: Internet
Author: User

# Include <iostream>
# Include <ctime>
# Include <cstdlib>
Using namespace STD;
// Display all balls
Inline void show (const int ball [], int Len ){
For (INT I = 0; I <Len; ++ I ){
Cout <ball [I] <"";
}
Cout <Endl;
}
// Initialize 13 balls
Inline void Init (INT ball [], int Len ){
For (INT I = 0; I <Len; ++ I ){
Ball [I] = 2;
}
}
// Insert a bad ball, which may be a light ball or a heavy ball. insert it to any position in the ball array.
Inline void pushbadball (INT ball [], int Len ){
Int badbils [] = {1, 3 };
Srand (time (0 ));
Int _ badball = badbils [rand () % 2]; // generate a global
Cout <"Bad ball:" <_ badball <Endl;
Ball [rand () % Len] = _ badball; // insert a bad ball
Show (ball, Len );
}
// Total of Arrays
Int sum (INT arr [], int begin, int end ){
Int sum = 0;
For (INT I = (begin-1); I <end; I ++ ){
Sum + = arr [I];
}
Return sum;
}

Void getbadball (INT ball [], int Len ){
// Group: four in group A, four in group B, and five in group C
Int A [4] = {BALL [0], ball [1], ball [2], ball [3]};
Int B [4] = {BALL [4], ball [5], ball [6], ball [7]};
Int C [5] = {BALL [8], ball [9], ball [10], ball [11], ball [12]};

Bool firstcheck = false; // compare with the first layer. If the value is false, the value true is greater
Bool secondecheck = false; // Layer 2 Comparison, same as above
Bool thirdcheck = false; // Layer 3 comparison, same as above
Bool ballflag = false; // false light, true heavy
Int badballindex = Len; // index of bad balls

If (sum (A,) = sum (B,) {// X indicates a bad ball, the same below, X in C
If (sum (A,) = sum (C,) {// X in C ()
If (A [0]! = C [3]) {
Badballindex = 11;
If (A [0]> C [3])
Ballflag = false;
Else
Ballflag = true;
} Else {
Badballindex = 12;
If (A [0] <C [4])
Ballflag = false;
Else
Ballflag = true;
}
}
Else {// X in C (1, 2, 3)
If (sum (A, 1, 3)> sum (C, 1, 3) // X is light
Ballflag = false;
Else // X is heavy
Ballflag = true;
If (C [0] = C [1]) {
Badballindex = 10;
} Else if (C [0]> C [1]) {
If (ballflag ){
Badballindex = 8;
} Else {
Badballindex = 9;
}
} Else {
If (ballflag ){
Badballindex = 9;
} Else {
Badballindex = 8;
}
}
}
} Else {// X in A or B
If (sum (A, 1, 4)> sum (B, 1, 4 ))
Firstcheck = true;
Else
Firstcheck = false;

If (sum (A,) + sum (B,) = sum (C,) {// X in A (4) or B (3, 4)
If (sum (A, 4, 4) + sum (B, 4, 4) = sum (C, 1, 2) {// X is B (3)
Badballindex = 6;
If (firstcheck) // if the first comparison is greater than, X is light, otherwise it is heavy
Ballflag = false;
Else
Ballflag = true;
} Else {
If (sum (A, 4, 4) + sum (B, 4, 4)> sum (C, 1, 2 ))//
Thirdcheck = true;
Else thirdcheck = false;
If (firstcheck ){
If (thirdcheck) {// The first big, the third big, prove that X is a (4) heavy
Badballindex = 3;
Ballflag = true;
}
Else {// large for the first time, small for the third time, proving that X is B (4) Light
Badballindex = 7;
Ballflag = false;
}
} Else {
If (! Thirdcheck) {// small for the first time, small for the third time, proving that X is a (4) Light
Badballindex = 3;
Ballflag = false;
}
Else {// small for the first time, large for the third time, proving that X is B (4) heavy
Badballindex = 7;
Ballflag = true;
}
}
}
} Else {// X in A (1, 3) or B (1, 2)
If (sum (A, 1, 3) + sum (B, 1, 2)> sum (C, 1, 5 )){
Secondecheck = true;
Ballflag = true;
} Else {
Secondecheck = false;
Ballflag = false;
}
If (firstcheck & ballflag) | // X indicates that X is a-large, and X is in a (1-3 ).
(! Firstcheck &&! Ballflag) {// X light a small, X in A (1-3)
If (A [0] = A [1]) {
Badballindex = 2;
} Else if (a [0]> A [1]) {
If (ballflag ){
Badballindex = 0;
} Else {
Badballindex = 1;
}
} Else {
If (ballflag ){
Badballindex = 1;
} Else {
Badballindex = 0;
}
}
} Else {// X indicates the weight of B, and X indicates the weight of B (1, 2 ).
If (B [0] = C [0]) {
Badballindex = 5;
} Else {
Badballindex = 4;
}
}
}

}

Cout <"Bad ball:" <(ballflag? 3:1) <Endl;
Cout <"Bad ball index (0-12):" <badballindex <Endl;
}

Int main (){
Const int num = 13;
Int ball [num] = {1 };
// Initialization
Init (ball, num );
// Insert a bad ball
Pushbadball (ball, num );
// Obtain the Bad ball
Getbadball (ball, num );

System ("pause ");
Return 1;
}

There are 13 balls, one of which is a bad ball X, which may be relatively light or heavy. How do we call it three times to find this bad ball X?

 

Divide 13 balls into three groups. A, B, C, and a have four balls, B has four balls, and C has five balls.

A: 1 2 3 4

B: 1 2 3 4

C: 1 2 3 4 5

 

In the first round, compare A and B

If a = B, compare a (1-3) and C (1-3) in the second round)

If! = B, then the second round compares a (1-3) + B (1-2) and C (1-5)

 

After the second round of comparison, one of the following three situations occurs, and the results can be obtained.

1. x exists in three balls in a group and knows that X is light or heavy.

2. x exists in two balls in a group. You do not need to know the importance of X.

3. x exists in two different groups. In a total of three balls, we need to compare the first one with the third one (the third one gets the weight of the ball, and then combine it with the first one ).

 

Result:

Bad ball: 3
2 2 2 2 2 2 2 2 2 2 2
Bad ball: 3
Bad ball index (0-12): 6
Press any key to continue...

Bad ball: 3
2 2 2 2 2 2 2 2 2 2 2 3 2
Bad ball: 3
Bad ball index (0-12): 11
Press any key to continue...

 

 

 

 

 

Related Article

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.