Zju2759 perfect weighing skill-balance weighing Problem

Source: Internet
Author: User

Description:

The weight quality of a balance is the Npower of 3, that is, 1, 3, 9, 27 ,...... Evaluate a measurement scheme to name an item with a mass of M (M <= 500000000 ). The weight can be placed on both sides of the balance.

Analysis:

If the data volume is small, you can use the search method to calculate all the measurement methods in advance and then create a table.

This item is of great quality. How can we determine which side of the weight is placed on the balance?

We can consider using a triple representation of item quality. See the following table:

M tridecimal weight 9 3 1(-1 indicates the same side as the item, 1 indicates the other side of the item, 0 does not need to be used)
1 0 0 1 0 1
2 0 0 2 0 1-1
3 0 1 0 0 1 0
4 0 1 1 0 1 1
5 0 1 2 1-1-1
6 0 2 0 1-1 0
7 0 2 1 1-1 1
8 0 2 2 1 0-1
9 1 0 0 1 0 0

We can see that there is a rule that the method of placing an item corresponds to the three-digit representation of the item quality.

Terminate the rule as follows:

Convert M to a triplicate system. In the conversion process,

If one digit is 0, no matter;

If one digit is 1, place a weight on the opposite side of the item. The weight quality is the base of the three-digit mechanism.

If one digit is 2, place a weight on the same side of the item and carry forward (m + = 1 ).

Do not have spaces at the end of the number sequence during output. It seems that it is not PE but wa.

Code:

/*
Zju2759
*/

# Include <stdio. h>
# Define N 20

Int s [N];

Void printa (int A [], int N ){
Int I;
For (I = 0; I <n; I ++ ){
If (I) printf ("");
Printf ("% d", a [I]);
}
Printf ("/N ");
}

Int main ()
{
Int I, J, K, X, M, N, R, P;
Int A [n], B [N];

// Ready
K = 1; s [0] = 1;
For (I = 1; I <20; I ++ ){
K * = 3;
S [I] = K;
}
N = 20;


While (scanf ("% d", & M )! = EOF)
{
R = 0; I = J = 0;
N = 0; P = 0;

While (m ){
R = m % 3;
M/= 3;
If (r = 2 ){
N ++;
B [j ++] = s [p];
M + = 1;
} Else if (r = 1 ){
A [I ++] = s [p];

}
P ++;
}

// Output
If (! J) printf ("-1/n ");
Else printa (B, j );
If (! I) printf ("-1/n ");
Else printa (a, I );
Puts ("");
}

Return 0;
}

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.