一行四个数字 (JQK 分别被替换成11,12,13)单空格分割,另一行输入 m
可以输出1
否则输出0
13 13 13 13 24
0
提示:
注意运算符优先级
from itertools import permutations while 1: try: nums = list(map(int, input().split())) m = int(input()) sta = False def dfs(nums, i, temp): if i == len(nums)-1: if eval(temp) == m: global sta sta = True return dfs(nums, i+1, temp+'+%d'%nums[i+1]) dfs(nums, i+1, temp+'-%d'%nums[i+1]) dfs(nums, i+1, temp+'*%d'%nums[i+1]) dfs(nums, i+1, temp+'//%d'%nums[i+1]) for ls in permutations(nums): dfs(ls, 0, 'nums[0]') if sta: break if sta: print('1') else: print('0') except EOFError: break
"""" DFS 对扑克顺序深搜,每种选择夹带4种情况运算符 暴力枚举应该也可以,扑克顺序4*3*2*1,运算符4*4*4, """ def dfs(ss, a, vis, m, step): for i in range(len(a)): if not vis[i]: continue vis[i] = False s = ss + a[i] if step == 3: if eval(s) == m: return True else: vis[i] = True continue res = dfs(s + '+', a, vis, m, step + 1) or dfs(s + '-', a, vis, m, step + 1) or \ dfs(s + '*', a, vis, m, step + 1) or dfs(s + '//', a, vis, m, step + 1) vis[i] = True if res: return True return False if __name__ == "__main__": a = input().strip().split() m = int(input()) vis = [True] * len(a) ans = dfs("", a, vis, m, 0) print(1 if ans else 0)
from itertools import permutations # 直接暴力枚举 def solve(nums, m): # 生成所有数字排列 for data in permutations(nums): for op1 in ['+', '-', '*', '//']: for op2 in ['+', '-', '*', '//']: for op3 in ['+', '-', '*', '//']: if m == eval(str(data[0]) + op1 + str(data[1]) + op2 + str(data[2]) + op3 + str(data[3])): return 1 return 0 nums = list(map(int, input().split())) m = int(input()) print(solve(nums, m))
#include <bits/stdc++.h> using namespace std; int m; set<int> S{0,1,2,3}; bool DFS(int *a, int p, int t){ if(S.empty()) return p+t==m; vector<int> v(S.begin(), S.end()); for(int i=0;i<v.size();i++){ int x = v[i]; S.erase(x); if(DFS(a, a[x], p+t)) return true; if(DFS(a, -a[x], p+t)) return true; if(DFS(a, p*a[x], t)) return true; if(DFS(a, p/a[x], t)) return true; S.insert(x); } return false; } int main(){ int a[4]; for(int i=0;i<4;i++) cin>>a[i]; cin>>m; cout<<DFS(a, 0, 0)<<endl; }
# 深度优先 from itertools import permutations data=[i for i in input().split()] target=int(input()) find=0 def rec(index,res): global find if len(res)==2*n-1: if eval("".join(res))==target&nbs***bsp;find: find=1 return else: return for i in ["+","-","*","//"]: res.append(i) res.append(cur[index]) rec(index+1,res) res[:]=res[:-2] n=len(data) for cur in permutations(data): if not find: rec(1,[cur[0]]) else: break print(find)
#include <iostream> #include <sstream> #include <climits> #include <vector> #include <string> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <algorithm> #include <stack> #include <queue> #include <stdio.h> using namespace std; bool dfs(const vector<int>& nums, unordered_set<int>& s, int p, int pp, int M) { if (s.empty()) return (p + pp) == M; vector<int> v(s.begin(), s.end()); for (auto i: v) { s.erase(i); if (dfs(nums, s, nums[i], p + pp, M)) return true; if (dfs(nums, s, -nums[i], p + pp, M)) return true; if (dfs(nums, s, p * nums[i], pp, M)) return true; if (dfs(nums, s, p / nums[i], pp, M)) return true; s.insert(i); } return false; } void solve(const vector<int>& nums, int M) { unordered_set<int> s{0, 1, 2, 3}; bool res = dfs(nums, s, 0, 0, M); cout << (res ? 1 : 0) << endl; } int main() { vector<int> nums(4, 0); for (int i = 0; i < 4; ++i) { cin >> nums[i]; } int M; cin >> M; solve(nums, M); return 0; }
arr = list(map(int, input().split(" "))) target = int(input()) expressions = [] def dfs(i, s): if(i == 4): expressions.append(s) return stemp = str(arr[i]) dfs(i + 1, s + "+" + stemp) dfs(i + 1, s + "-" + stemp) dfs(i + 1, s + "*" + stemp) dfs(i + 1, s + "//" + stemp) def solve(): s = str(arr[0]) expressions.clear() dfs(1, s) flag = True for e in expressions: if(eval(e) == target): return True return False def Prim(i): if i == 4: if solve(): return True else: return False k = i while k < 4: arr[k], arr[i] = arr[i], arr[k] if Prim(i + 1): return True arr[k], arr[i] = arr[i], arr[k] k += 1 return False if Prim(0): print("1") else: print("0")
# -*- coding: utf-8 -*- """ Created on Mon Apr 1 00:54:31 2019 @author: Administrator """ data = [int (i) for i in input().split()] m = int(input()) def f(n1,n2,op): if op=="+": return n1+n2 elif op=="-": return n1-n2 elif op=="*": return n1*n2 elif op=="/": if n2 == 0: return -999999 return n1//n2 def cal(data): flag = True while flag: flag = False for i in range(len(data)): if data[i] in ["*","/"]: # print(data[i]) data = data[0:i-1] + [f(data[i-1],data[i+1],data[i])] + data[i+2:] flag = True break while len(data)>1: for i in range(len(data)): if data[i] in ["+","-"]: data = data[0:i-1] + [f(data[i-1],data[i+1],data[i])] + data[i+2:] break # print(data) return data[0] #print(cal([1,"+",2,"-",5])) flag = 0 def A(data): if len(data)==1: return [data] else: finalRes = [] for i in range(len(data)): newData = data[0:i]+data[i+1:] res = A(newData) for j in range(len(res)): res[j].insert(0,data[i]) finalRes.extend(res) return finalRes for op1 in ["+","-","*","/"]: for op2 in ["+","-","*","/"]: for op3 in ["+","-","*","/"]: for nums in A(data): res = cal([nums[0],op1,nums[1],op2,nums[2],op3,nums[3]]) if res == m: flag = 1 print(flag)