1. 程式人生 > >計算結果總是100的程式

計算結果總是100的程式

編寫一個在1,2,…,9(順序不能變)數字之間插入+或-或什麼都不插入,使得計算結果總是100的程式,並輸出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100。

執行環境: python 3.4.3

執行命令: python3 test.py


執行結果:

1 + 2 + 3 - 4 + 5 + 6 + 7 8 + 9 = 100
1 + 2 + 3 4 - 5 + 6 7 - 8 + 9 = 100
1 + 2 3 - 4 + 5 + 6 + 7 8 - 9 = 100
1 + 2 3 - 4 + 5 6 + 7 + 8 + 9 = 100
1 2 + 3 + 4 + 5 - 6 - 7 + 8 9 = 100
1 2 + 3 - 4 + 5 + 6 7 + 8 + 9 = 100
1 2 - 3 - 4 + 5 - 6 + 7 + 8 9 = 100
total : 6561---7


test.py


#! /usr/bin/python3
# -*- coding:utf-8 -*-

'''
編寫一個在1,2,…,9(順序不能變)數字之間插入+或-或什麼都不插入,使得計算結果總是100的程式,並輸出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100。
test100.py
python 
'''
import sys
def nullcheck(data):
	
	back=0;
	for i in range(len(data)):

		if i%2 == 0 :
			continue
		else:												
			if data[i] == ' ' and i-back == 2:
				return -1
			elif data[i]== ' ':
				back = i						
	return 0	
	
def check100(data):
	value =0
	num = 0
	j = 0
	n = 0
	op = 0
	for i in range(len(data)):

		if data[i] == '+' or data[i] == '-' or data[i] == '=':
			if op == '+':
				#print("i=%d,j=%d"%(i,j))
				if i-j == 1 :
					#print("+{0}".format(data[j]))
					value = value + data[j]
				elif i-j > 1 and n > 0:
	
					num = 0
					for k in range(i-j):
						if data[k+j] != ' ':
							#print("i={0},j={1},k={2},data[]={3}".format(i,j,k,data[k+j]))
							
							num = (10**((i-j-k+1)//2 - 1) )*data[k+j] + num
							
					#print(" +{0}".format(num))
					value = value + int(num)
			elif op == '-':
				if i-j == 1:
					#print("-{0}".format(data[j]))
					value = value - data[j]
					
				elif i-j > 1 and n > 0:
					num = 0
					for k in range(i-j):
						#print("i={0},j={1},k={2},data[{3}]={4}".format(i,j,k,k+j,data[k+j]))
						if data[k+j] != ' ':
							num = (10**((i-j-k +1)/2 - 1) )*data[k+j] + num
						
					#print(" -{0}".format(num))
					value = value - int(num)
			elif op == 0:
				if i-j == 1:
					#print("#{0}".format(data[j]))
					value = value + data[j]
					
				elif i-j > 1 and n > 0:
					num = 0
					for k in range(i-j):
						#print("i={0},j={1},k={2},data[{3}]={4}".format(i,j,k,k+j,data[k+j]))
						if data[k+j] != ' ':
							num = (10**((i-j-k +1)/2 - 1) )*data[k+j] + num
						
					#print("#{0}".format(num))
					value = value + int(num)
					
			
				
					
			j = i+1
			n = 0
			op = 0
			
		if i%2 ==0:
			continue
		else:
			
			if data[i] == '+':
				op = data[i]														
			elif data[i] == '-':
				op = data[i]	
			elif data[i] == ' ':
				n = n + 1
				

	if value == 100:
		#print("{0}={1:d}".format(data,value));
		return 100
	else:
		#print("{0}={1:d}".format(data,value));
		return value
	
	
def DUMP_100(data):
	for i in range(len(data)):
		if(data[i]!=' '):
			print(data[i],end=' ')	

					
	print("100")
	
	
def test100():
	data=[1,2,3,4,5,6,7,8,9]
	op=['+','-',' ']
	temp=[]
	cnt = 0
	t = 0
	
	for i1 in range(len(op)):
		for i2 in range(len(op)):
			for i3 in range(len(op)):
				for i4 in range(len(op)):
					for i5 in range(len(op)):
						for i6 in range(len(op)):
							for i7 in range(len(op)):
								for i8 in range(len(op)):
										#print("1{0}2{1}3{2}4{3}5{4}6{5}7{6}8{7}9".format(op[i1],op[i2],op[i3],op[i4],op[i5],op[i6],op[i7],op[i8]))
										temp.clear()
										temp.append(data[0])
										temp.append(op[i1])
										temp.append(data[1])
										temp.append(op[i2])
										temp.append(data[2])
										temp.append(op[i3])
										temp.append(data[3])
										temp.append(op[i4])
										temp.append(data[4])
										temp.append(op[i5])
										temp.append(data[5])
										temp.append(op[i6])
										temp.append(data[6])
										temp.append(op[i7])
										temp.append(data[7])
										temp.append(op[i8])
										temp.append(data[8])								
										temp.append("=")
										t = t + 1
										if nullcheck(temp) != 0:
											continue
										
										if check100(temp) != 100:
											continue	
										
										DUMP_100(temp)
										cnt= cnt +1
										
	print("total : {0}---{1}".format(t,cnt));
										

	
	
	
if __name__ == '__main__':
	if len(sys.argv) > 1:
		check100([1, '+', 2, '+', 3, ' ', 4, '-', 5, '+', 6, ' ', 7, '-', 8, ' ', 9,"="]);
	else:
		test100()