问题陈述:
有一个坏了的计算器。只有少数位数0 to 9和运算符+, -, *, /正在工作。
不对。需要使用工作位数和操作符形成。键盘上的每一个按键都被称为操作。
=运算符总是工作的,并在req时使用。由运算符组成。-1需要打印,以防req。不能使用数字形成,并且提供的运算符OR超过了最大值no。允许的行动。0 <= calcno <= 999输入:
1代表+,2代表-,3代表*,4代表/。输出:
查找最低要求的操作,以形成req。
示例:
输入1:
2 1 8
2 5
3
50 可能的方法:
案例1:2*5*5 = -> 6 operations
案例2:2*25 = -> 4 operations
4是问题的答案
输入2:
3 4 8
5 4 2
3 2 4 1
42 可能的方法:
案例1:42 -> 2 operations (直接键)
案例2:5*4*2+2 = -> 8 operations
..........some其他方式
2是问题的答案
我对这个问题没有得到适当的处理。
有人能建议一些方法来解决这个问题吗。
发布于 2021-11-09 10:10:48
#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;
}https://stackoverflow.com/questions/43224443
复制相似问题