暴力枚举 + 24点 --- hnu : Cracking the Safe
Cracking the Safe |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB |
Total submit users: 46, Accepted users: 12 |
Problem 12886 : No special judgement |
Problem description |
Secret agent Roger is trying to crack a safe containing evil Syrian chemical weapons. In order to crack the safe, Roger needs to insert a key into the safe. The key consists of four digits. Roger has received a list of possible keys from his informants that he needs to try out. Trying out the whole list will take too long, so Roger needs to find a way to reduce the list. |
Input |
On the first line one positive number: the number of test cases, at most 100.
After that per test case: |
Output |
Per test case: |
Sample Input |
4 4 7 8 8 1 1 2 4 1 1 1 1 1 3 4 6 |
Sample Output |
YES NO NO YES |
Problem Source |
BAPC preliminary 2013 |
Mean:
给你4个数,你需要判断这4个数是否能够通过"+"、"-"、"*"、"/"四种得到24。
analyse:
数据这么小,直接暴力枚举。
先枚举四个数的位置,再枚举三个运算符,最后枚举运算顺序。
解法二:递归求解。
Time complexity:4*4*4*4*4*4*4*5=81920,不超过81920次
Source code:
// 1143K 27MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAX 1100
#define LL long long
using namespace std;
bool mark;
double num[5];
double aa[5];
double calc(double a,double b,int flag)
{
switch(flag)
{
case 1:return (a+b);
case 2:return (a-b);
case 3:return (a*b);
case 4:return (a/b);
}
}
void algebra()
{
double tmp1,tmp2,tmp3;
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
for(int k=1;k<=4;k++)
{
// 运算顺序1
tmp1=calc(aa[1],aa[2],i);
tmp2=calc(tmp1,aa[3],j);
tmp3=calc(tmp2,aa[4],k);
if(fabs(tmp3-24)<1e-6)
{
mark=1;
return ;
}
// 运算顺序2
tmp1=calc(aa[1],aa[2],i);
tmp2=calc(aa[3],aa[4],k);
tmp3=calc(tmp1,tmp2,j);
if(fabs(tmp3-24)<1e-6)
{
mark=1;
return ;
}
// 运算顺序3
tmp1=calc(aa[2],aa[3],j);
tmp2=calc(aa[1],tmp1,i);
tmp3=calc(tmp2,aa[4],k);
if(fabs(tmp3-24)<1e-6)
{
mark=1;
return ;
}
// 运算顺序4
tmp1=calc(aa[2],aa[3],j);
tmp2=calc(tmp1,aa[4],k);
tmp3=calc(tmp2,aa[1],i);
if(fabs(tmp3-24)<1e-6)
{
mark=1;
return ;
}
// 运算顺序5
tmp1=calc(aa[3],aa[4],k);
tmp2=calc(aa[2],tmp1,j);
tmp3=calc(aa[1],tmp2,i);
if(fabs(tmp3-24)<1e-6)
{
mark=1;
return ;
}
}
}
}
}
void arrange()
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if(j==i)continue;
for(int k=1;k<=4;k++)
{
if(k==i||k==j)continue;
for(int l=1;l<=4;l++)
{
if(l==i||l==j||l==k)continue;
aa[1]=num[i],aa[2]=num[j],aa[3]=num[k],aa[4]=num[l];
algebra();
}
}
}
}
}
int main()
{
int T;
cin>>T;
while(T--)
{
mark=false;
for(int i=1;i<=4;i++)
cin>>num[i];
arrange();
if(mark)
puts("YES");
else
puts("NO");
}
return 0;
}
递归:
// 724K 0MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAX 1100
#define LL long long
using namespace std;
double num[4];
bool solve ( int n ) {
if ( n == 1 ) {
if ( fabs ( num[0] - 24 ) < 1E-6 )
return true;
else
return false;
}
for ( int i = 0; i < n; i++ )
{
for ( int j = i + 1; j < n; j++ )
{
double a, b;
a = num[i];
b = num[j];
num[j] = num[n - 1];
num[i] = a + b;
if ( solve ( n - 1 ) )
return true;
num[i] = a - b;
if ( solve ( n - 1 ) )
return true;
num[i] = b - a;
if ( solve ( n - 1 ) )
return true;
num[i] = a * b;
if ( solve ( n - 1 ) )
return true;
if ( b != 0 ) {
num[i] = a / b;
if ( solve ( n - 1 ) )
return true;
}
if ( a != 0 ) {
num[i] = b / a;
if ( solve ( n - 1 ) )
return true;
}
num[i] = a;
num[j] = b;
}
}
return false;
}
int main() {
int x;
int T;
cin >> T;
while ( T-- ) {
for ( int i = 0; i < 4; i++ ) {
cin >> x;
num[i] = x;
}
if ( solve ( 4 ) ) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/3903431.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?