about summary refs log tree commit diff
path: root/2ndary/12/TP-ThanhHoá-2009/bai4.py
diff options
context:
space:
mode:
Diffstat (limited to '2ndary/12/TP-ThanhHoá-2009/bai4.py')
-rw-r--r--2ndary/12/TP-ThanhHoá-2009/bai4.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/2ndary/12/TP-ThanhHoá-2009/bai4.py b/2ndary/12/TP-ThanhHoá-2009/bai4.py
new file mode 100644
index 0000000..f5c5a26
--- /dev/null
+++ b/2ndary/12/TP-ThanhHoá-2009/bai4.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python3
+
+
+def ops(number, length):
+    b = bin(number)[2:]
+    return '+' * (length - len(b)) + b.replace('0', '+').replace('1', '-')
+
+
+def libai4(n):
+    seq, l = list(range(1, n + 1)), []
+
+    for i in range(2 ** (n - 1)):
+        s = ''.join(["{}{}".format(*j) for j in zip(ops(i, n), seq)])[1:]
+        if eval(s) == 0:
+            l.append(s + '=0\n')
+
+    return l
+
+
+if __name__ == '__main__':
+    with open('BAI4.INP') as f:
+        n = int(f.read())
+
+    with open('BAI4.OUT', 'w') as f:
+        l = libai4(n)
+        f.write('{}\n'.format(len(l)))
+        for s in l:
+            f.write(s)