forked from keshavnandan/Topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApothecary.html
18 lines (17 loc) · 3.79 KB
/
Apothecary.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>    </td><td><p>
An accurate scale is one of the most important tools of the apothecary (an old-time pharmacist).
To measure the weight of an object, the apothecary places the object on one
pan of the scale, along with some weights of known size, and adds more weights
of known size to the other pan until the scales balance. For example, if an object
weighs 17 grains, the apothecary could balance the scales by placing a 1-grain weight
and a 9-grain weight in the pan with the object, and a 27-grain weight in the other
pan.
</p>
<p>
The apothecary owns weights in a range of sizes starting at 1 grain. In particular, he owns one weight for each
power of 3: 1 grain, 3 grains, 9 grains, 27 grains, etc. Determine, for an object weighing <b>W</b> grains, how to distribute the weights among the
pans to balance the object. This distribution will be unique. Return a vector <int> of the weights used. The sign of each weight should be
negative if the weight goes in the same pan as the object, and positive if it goes in the other pan. The vector <int> should be arranged in increasing order.
</p>
</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>    </td><td><table><tr><td>Class:</td><td>Apothecary</td></tr><tr><td>Method:</td><td>balance</td></tr><tr><td>Parameters:</td><td>int</td></tr><tr><td>Returns:</td><td>vector <int></td></tr><tr><td>Method signature:</td><td>vector <int> balance(int W)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>    </td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>64</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>W</b> is between 1 and 1000000, inclusive.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>17</pre></td></tr></table></td></tr><tr><td><pre>Returns: { -9, -1, 27 }</pre></td></tr><tr><td><table><tr><td colspan="2">The example above.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>1</pre></td></tr></table></td></tr><tr><td><pre>Returns: { 1 }</pre></td></tr><tr><td><table><tr><td colspan="2">A 1-grain weight is placed in the pan opposite the object being measured.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>2016</pre></td></tr></table></td></tr><tr><td><pre>Returns: { -243, -9, 81, 2187 }</pre></td></tr><tr><td><table><tr><td colspan="2">A 9-grain weight and a 243-grain weight are placed in the pan with the object,
and an 81-grain weight and a 2187-grain weight are placed in the opposite pan.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>1000000</pre></td></tr></table></td></tr><tr><td><pre>Returns: { -531441, -59049, -6561, -243, -27, 1, 81, 729, 2187, 1594323 }</pre></td></tr><tr><td></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>