Comments (5)
Given a thing weighting x gram, we first convert it to 3-based representation, e.g. 55 in decimal to 2001 in 3. Check each digit of the representation, if the digit is 1, it means that the weight corresponding to this digit is put on the other side of the balance. If the digit is 2, we add 1 to this digit, and it means the weight corresponding to this digit it put on the same side of the thing. Continue till the last digit, and we get the answer.
The following Python program is the example code:
#! /usr/bin/env python
# num is a integer >= 0
# return a list containing the digits in base
# the digits are ordered from lower bits to higher bits
def convert_to_based_num(num, base):
digits = []
while num > 0:
quotient, remainder = num / base, num % base
digits.append(remainder)
num = quotient
return digits
def add_3_based_digits(digits, index, num):
while digits[index] + num >= 3:
digits[index] = (digits[index] + num) % 3
num = 1
index += 1
if index >= len(digits):
digits.append(0)
digits[index] += num
def balance(digits):
left = []
for i, d in enumerate(digits):
if d == 2:
add_3_based_digits(digits, i, 1)
left.append(1)
else:
left.append(0)
return left, digits
def print_balance(num, left, right):
print "Output:", num,
for i, d in enumerate(left):
if d != 0:
print "+ 3^%d" % i,
print "= 3^" + " + 3^".join([str(i) for i, d in enumerate(right) if d != 0])
if __name__ == '__main__':
w = int(raw_input("Input: "))
left, right = balance(convert_to_based_num(w, 3))
print_balance(w, left, right)
from algohacks.
55 = 2_(3^3)+3^0
-->
55+3^3 = 3_(3^3)+3^0
-->
55+3^3 = 3^4+3^0
from algohacks.
@kmalloc What do you mean?
from algohacks.
solution to the problem.
from algohacks.
@kmalloc Yeah, the point is to transform a given number to 3 based representation.
from algohacks.
Related Issues (10)
- Possible Ways To Transform HOT 1
- Hacking Spot Vol.1 Issue 4: Min-stack HOT 7
- Hacking Spot Vol.1 Issue 5: Least positive number HOT 3
- Hacking Spot Vol.1 Issue 6: Max Rectangle in Histogram HOT 1
- Hacking Spot Vol.2 Issue 1: Square
- Hacking Spot Vol.2 Issue 2: Nth element
- Rotate an array
- Merge Array Inplace
- Largest Set of Consecutive Number
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from algohacks.