about summary refs log tree commit diff
path: root/12/TP-ThanhHoá-2009/bai4.py
diff options
context:
space:
mode:
authorRaphael McSinyx <vn.mcsinyx@gmail.com>2016-11-19 19:00:47 +0700
committerRaphael McSinyx <vn.mcsinyx@gmail.com>2016-11-19 20:46:04 +0700
commitf97ad3e9e225a288e7a8a53c2ef0acb45a47dbde (patch)
tree4d2d7ff6fd480396ed862069e7edc26fa7b66ccd /12/TP-ThanhHoá-2009/bai4.py
parent10de10a507238f3be43dee304e057679b5bb2736 (diff)
downloadcp-f97ad3e9e225a288e7a8a53c2ef0acb45a47dbde.tar.gz
Thêm đề 12 Thanh Hoá 2008-2009
Diffstat (limited to '12/TP-ThanhHoá-2009/bai4.py')
-rwxr-xr-x12/TP-ThanhHoá-2009/bai4.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/12/TP-ThanhHoá-2009/bai4.py b/12/TP-ThanhHoá-2009/bai4.py
new file mode 100755
index 0000000..f5c5a26
--- /dev/null
+++ b/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)