首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >断裂计算器

断裂计算器
EN

Stack Overflow用户
提问于 2017-04-05 07:20:38
回答 1查看 2.4K关注 0票数 0

问题陈述:

有一个坏了的计算器。只有少数位数0 to 9和运算符+, -, *, /正在工作。

不对。需要使用工作位数和操作符形成。键盘上的每一个按键都被称为操作。

  • =运算符总是工作的,并在req时使用。由运算符组成。
  • -1需要打印,以防req。不能使用数字形成,并且提供的运算符OR超过了最大值no。允许的行动。
  • 在计算结果的过程中,没有时间点的no。应转为负数或超过999 0 <= calcno <= 999

输入:

  • 第一行包含3个空格分隔的nos: no。工作位数,不。最大限度的工作人员。不允许操作。
  • 第2行包含空格分隔的工作数字。
  • 第3行包含分隔的工作操作符1代表+2代表-3代表*4代表/
  • 第4行包含req。不需要组建。

输出:

查找最低要求的操作,以形成req。

示例:

输入1:

代码语言:javascript
复制
2 1 8  
2 5  
3  
50 

可能的方法:

案例1:2*5*5 = -> 6 operations

案例2:2*25 = -> 4 operations

4是问题的答案

输入2:

代码语言:javascript
复制
3 4 8  
5 4 2  
3 2 4 1  
42  

可能的方法:

案例1:42 -> 2 operations (直接键)

案例2:5*4*2+2 = -> 8 operations

..........some其他方式

2是问题的答案

我对这个问题没有得到适当的处理。

有人能建议一些方法来解决这个问题吗。

EN

回答 1

Stack Overflow用户

发布于 2021-11-09 10:10:48

代码语言:javascript
复制
    #include <bits/stdc++.h>
using namespace std;

int main() {
    // your code goes here
    int n,m,o;
    cin>>n>>m>>o;
    int arr[n];
    queue<pair<int,int>> q;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        q.push(make_pair(arr[i],1));
    }
    int op[m];
    for(int i=0;i<m;i++) cin>>op[i];
    unordered_map<int,int> mp;
    for(int i=0;i<m;i++) mp[op[i]]=1;
    int target;
    cin>>target;
    int ans=INT_MAX;
    while(!q.empty())
    {
        int num=q.front().first;
        int count=q.front().second;
        if(num==target) ans=min(ans,count);
        q.pop();
        for(int i=0;i<=4;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(i==0 and count+1<=o)
                {
                    q.push(make_pair(num*10+arr[j],count+1));
                }
                else
                {
                    if(i==1 and mp.find(i)!=mp.end() and count+3<=o)
                    {
                        q.push(make_pair(num+arr[j],count+3));
                    }
                    if(i==2 and mp.find(i)!=mp.end() and count+3<=o)
                    {
                        q.push(make_pair(abs(num-arr[j]),count+3));
                    }
                    if(i==3 and mp.find(i)!=mp.end() and count+3<=o)
                    {
                        q.push(make_pair(num*arr[j],count+3));
                    }
                    if(i==4 and mp.find(i)!=mp.end() and count+3<=o)
                    {
                        q.push(make_pair(num/arr[j],count+3));
                    }
                }
            }
        }
    }
    if(ans==INT_MAX) cout<<"-1"<<endl;
    else cout<<ans<<endl;
    
    return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43224443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档