Python Code:
@ 此code是針對(b),但這題目的數字太大了,跑不出來,數字小一點才可以!!!
import numpy as np
import time
n =4
s = np.array(range(1,n+1))
#print(s)
tStart = time.time()#計時開始
flag=0
for i in range(2 ** len(s)):#子集的個數
combine = []
for j in range(len(s)):#用來判斷二進制數的下標爲j的位置的數是否爲1
if (i >> j) % 2:
combine.append(s[j])
for k in range(0,len(combine)):
for l in range(k+1,len(combine)):
if (combine[k]+combine[l])==3:
print(combine)
flag+=1
else:
continue
tEnd = time.time()#計時結束
print((2**n)-flag)
print("It cost %f sec" % (tEnd - tStart))#會自動做近位
print("請輸入一個集合,將列所有的子集合:")
s=input().split()
sk=[x for x in s]
n=len(sk)
for i in range(2**n):
subset=[]
for j in range(n):
if (i>>j)%2:
subset.append(sk[j])
print(subset)
答案:(1)2184年 (2)77人
Python Code:
two=[]
for i in range(100):
two.append(2**i)
for a in range(1,100):
for b in range(1,100):
for c in range(1,100):
s1=a*b-c
s2=b*c-a
s3=c*a-b
if (s1>0) and (s2>0) and (s3>0):
if (s1 in two) and (s2 in two) and (s3 in two):
print(a,b,c)
答案:
2 2 3
2 3 2
2 6 11
2 11 6
3 2 2
3 5 7
3 7 5
5 3 7
5 7 3
6 2 11
6 11 2
7 3 5
7 5 3
11 2 6
11 6 2