方法1 :python 枚举遍历法
def GetResult(): factor = [5,3,1/3] L = [] for gj in range(100//factor[0]): mj_num = (100 - factor[0]*gj)//factor[1] for mj in range(mj_num): jc_num = int((100 - factor[0]*gj - factor[1]*mj)//factor[2]) if (gj+mj+jc_num) == 100: L.append([gj,mj,jc_num]) for x in L: for xx in x: print(xx,end=' ') print() if L !=[]: return 0 else: return -1 while True: try: a = int(input()) GetResult() except: break
方法2:python 函数法
while True: try: n = int(input()) # 鸡公最多买20只 for x in range(21): y = (100-7*x)/4 # 鸡母的数量 z = 100 - x - y # 鸡雏的数量 if y == int(y) and y >= 0 and z >= 0: print(x, int(y), int(z)) except : break