Game Show Math - UVa10400 搜索

本文详细介绍了如何通过搜索方法解决给定数字序列和目标数值之间的数学表达式生成问题,确保表达式遵循特定限制条件并能准确计算目标数值。

Game Show Math
Input:
 standard input
Output: standard output
Time Limit: 15 seconds

A game show in Britain has a segment where it gives its contestants a sequence of positive numbers and a target number. The contestant must make a mathematical expression using all of the numbers in the sequence and only the operators: +-*, and, /. Each number in the sequence must be used exactly once, but each operator may be used zero to many times. The expression should be read from left to right, without regard for order of operations, to calculate the target number. It is possible that no expression can generate the target number. It is possible that many expressions can generate the target number.

There are three restrictions on the composition of the mathematical expression:

o  the numbers in the expression must appear in the same order as they appear in the input file

o  since the target will always be an integer value (a positive number), you are only allowed to use / in the expression when the result will give a remainder of zero.

o  you are only allowed to use an operator in the expression, if its result after applying that operator is an integer from (-32000 ..+32000).

Input

The input file describes multiple test cases. The first line contains the number of test cases n.

Each subsequent line contains the number of positive numbers in the sequence p, followed by p positive numbers, followed by the target number. Note that 0 < p £ 100. There may be duplicate numbers in the sequence. But all the numbers are less than 32000.

Output

The output file should contain an expression, including all k numbers and (k-1) operators plus the equals sign and the target. Do not include spaces in your expression. Remember that order of operations does not apply here. If there is no expression possible output "NO EXPRESSION" (without the quotes). If more than one expression is possible, any one of them will do.

 

Sample Input

3
3 5 7 4 3
2 1 1 2000
5 12 2 5 1 2 4

Sample Output
5+7/4=3 
NO EXPRESSION
12-2/5*1*2=4


题意:用给定的数字使用+-*/来得到最后一个数。

思路:因为要求中间数值在+-3200之内,所以用搜索的方法就可以了。

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
int T,t,n,vis[110][64010],pre[110][54010],val[110],m=32000;
bool flag;
void dfs(int pos,int fa,int num,int type)
{
    if(num<=-m || num>=m)
      return ;
    if(vis[pos][num+m] || flag)
      return;
    vis[pos][num+m]=type;
    pre[pos][num+m]=fa;
    if(pos==n)
    {
        if(num==val[n+1])
          flag=true;
        return;
    }
    int num2;
    num2=num+val[pos+1];dfs(pos+1,num,num2,1);
    num2=num-val[pos+1];dfs(pos+1,num,num2,2);
    num2=num*val[pos+1];dfs(pos+1,num,num2,3);
    num2=num/val[pos+1];dfs(pos+1,num,num2,4);
}
void print(int pos,int num)
{
    int num2=num+m;
    if(pos>1)
      print(pos-1,pre[pos][num2]);
    if(vis[pos][num2]==1)
      printf("+");
    else if(vis[pos][num2]==2)
      printf("-");
    else if(vis[pos][num2]==3)
      printf("*");
    else if(vis[pos][num2]==4)
      printf("/");
    printf("%d",val[pos]);
}
int main()
{
    int i,j,k;
    scanf("%d",&T);
    for(t=1;t<=T;t++)
    {
        scanf("%d",&n);
        for(i=1;i<=n+1;i++)
           scanf("%d",&val[i]);
        memset(vis,0,sizeof(vis));
        flag=false;
        dfs(1,0,val[1],0);
        if(flag)
        {
            print(n,val[n+1]);
            printf("=%d\n",val[n+1]);
        }
        else
          printf("NO EXPRESSION\n");
    }
}



### 问题分析 `Error: Unable to access jarfile math-game.jar` 表示系统尝试运行 `math-game.jar` 文件时失败,可能的原因包括但不限于以下几种: 1. **文件路径错误**:指定的 JAR 文件路径不正确或者不存在。 2. **权限不足**:当前用户没有足够的权限访问该文件。 3. **环境变量配置问题**:如果依赖于某些环境变量(如 `JAVA_HOME` 或者 `Path`),可能存在配置错误。 4. **JDK 配置问题**:使用的 JDK 可能未正确安装或配置。 以下是针对此问题的具体解决方案以及相关说明。 --- ### 解决方案 #### 1. 检查文件是否存在 确认 `math-game.jar` 是否存在于指定路径下。可以通过命令行验证: ```bash ls /path/to/math-game.jar ``` 如果没有找到文件,则需要重新上传或生成该文件[^1]。 #### 2. 确认文件路径是否正确 确保执行命令时指定了正确的绝对路径或相对路径。例如: ```bash java -jar /absolute/path/to/math-game.jar ``` 如果是相对路径,请先切换到包含 `math-game.jar` 的目录再执行: ```bash cd /path/to/directory java -jar math-game.jar ``` #### 3. 检查文件权限 通过以下命令查看文件权限并设置合适的权限: ```bash ls -l /path/to/math-game.jar chmod +rwx /path/to/math-game.jar ``` 如果文件位于受限目录中,还需要调整目录权限: ```bash chmod +rx /path/to/directory ``` #### 4. 验证 JDK 安装与配置 根据引用中的描述,当前环境中已安装 OpenJDK 1.8.0_242。可以再次验证 Java 版本和路径配置是否正常: ```bash java -version echo $JAVA_HOME ``` 如果发现 `$JAVA_HOME` 不指向实际 JDK 路径,需修改环境变量配置。对于 Windows 用户,可以在系统属性 -> 环境变量中更新 `JAVA_HOME` 和 `Path` 值为新 JDK 的安装路径。 #### 5. 使用 jstack 排查线程状态 如果有其他进程干扰导致无法加载 JAR 文件,可借助 `jstack` 工具排查线程阻塞情况。例如: ```bash jstack <PID> | grep -A 10 "main" ``` 其中 `<PID>` 是目标 Java 进程 ID。从日志中查找是否有异常行为影响了资源读取[^2]。 #### 6. 尝试修复损坏的 JAR 文件 如果怀疑 `math-game.jar` 文件本身存在问题,可以用工具校验其完整性: ```bash unzip -t math-game.jar ``` 若显示有错误提示,则重新打包或替换原始文件。 --- ### 总结 综合以上方法逐一排查可能导致 `Unable to access jarfile` 错误的因素,并采取相应措施解决问题。重点在于核实文件存在性和可用性、检查操作权限及环境配置准确性。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值