diff options
author | Nguyễn Gia Phong <mcsinyx@disroot.org> | 2020-06-06 21:33:13 +0700 |
---|---|---|
committer | Nguyễn Gia Phong <mcsinyx@disroot.org> | 2020-06-06 21:33:13 +0700 |
commit | 2f674dc80f0382f1c3178f435714960734dc9d3c (patch) | |
tree | 2abba7e4ec72bd16f58f7375126144d3fd9f4bca /2ndary/12/TP-ThanhHoá-2009/bai4.py | |
parent | b2d80610db6beda38573890ed169815e495bc663 (diff) | |
download | cp-2f674dc80f0382f1c3178f435714960734dc9d3c.tar.gz |
Reorganize stuff from secondary school
Diffstat (limited to '2ndary/12/TP-ThanhHoá-2009/bai4.py')
-rw-r--r-- | 2ndary/12/TP-ThanhHoá-2009/bai4.py | 28 |
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) |