uva 10400 Game Show Math
来源:互联网 发布:win10解压缩软件 编辑:程序博客网 时间:2024/06/05 20:18
题意:满足表达式,用DFS记忆化搜索,把到当前运算符所得的结果记录下来,起初用回溯一直错
#include <cstdio>#include <cmath>#include <cstring>#include <iostream>using namespace std;int flag,n,b[101],ans,a[101],vis[100][64001];void dfs(int sum,int deep){ if (deep > n && sum == ans) { flag=1; return; } if (flag || deep > n || abs(sum) > 32000) return; for (int i = 1; i <= 4; i++) { b[deep-1] = i; int s = 32001; if (i == 1) s = sum + a[deep]; if (i == 2) s = sum - a[deep]; if (i == 3) s = sum * a[deep]; if (i == 4 && (sum % a[deep] == 0)) s = sum / a[deep]; if (abs(s) <=32000 && vis[deep-1][s] == 0) { vis[deep-1][s]=1; dfs(s,deep+1); } if (flag) return; }}int main(){ int t; scanf("%d",&t); while (t--) { scanf("%d",&n); for (int i = 1; i <= n; i++) scanf("%d",&a[i]); scanf("%d",&ans); flag=0; memset(vis,0,sizeof(vis)); dfs(a[1],2); if (flag) { for (int i =1 ; i < n; i++) { printf("%d",a[i]); if (b[i] == 1) printf("+"); if (b[i] == 2) printf("-"); if (b[i] == 3) printf("*"); if (b[i] == 4) printf("/"); } printf("%d=%d\n",a[n],ans); } else printf("NO EXPRESSION\n"); } return 0;}
- UVa 10400 - Game Show Math
- uva 10400 - Game Show Math
- uva 10400 - Game Show Math
- UVa 10400 - Game Show Math
- UVA 10400 - Game Show Math
- uva 10400 Game Show Math
- uva 10400 Game Show Math
- UVA 10400 Game Show Math
- uva 10400 Game Show Math
- UVa 10400 - Game Show Math
- uva 10400 - Game Show Math
- UVA - 10400 Game Show Math
- UVa 10400 - Game Show Math
- UVA - 10400 Game Show Math
- uva 10400(Game Show Math) (DP)
- UVA - 10400 Game Show Math(回溯)
- UVA - 10400 Game Show Math DFS
- uva 10400 Game Show Math(深搜 )
- paip.输入法编程--词频调整原则--发音长度优先
- Java连接Oracle笔记
- 《Windows核心编程》第二讲 Unicode(2)成为符合ANSI和Unicode的应用程序
- java 对数组进行插入删除修改
- 寻找正在连接中的网络连接,并开启网络连接的网络连接共享功能
- uva 10400 Game Show Math
- Android中获取应用程序(包)的信息-----PackageManager的使用(一)
- Android display架构分析
- 适配器模式
- 安卓开发32:Paint 画笔
- 免费 开发资源库 android web
- 裁剪空间法--实现细节
- java反射机制
- Category(范畴)的使用方法与介绍