#include <fstream>
#include <cstring>
using namespace std;
ifstream fin("point24.in");
ofstream fout("point24.out");
typedef char String[20];
struct oppath{
int a,b;
char op;
};
char chars[4];
int shu[4];
oppath paths[4];
String ans[4] ,buffer;
String minans="ZZZZZ";
void output(){
for(int i=0;i<4;i++) sprintf(ans[i],"%c",chars[i]);
for(int i=0;i<3;i++){
sprintf(buffer,"(%s%c%s)",ans[paths[i].a],paths[i].op,ans[paths[i].b]);
sprintf(ans[paths[i].a],"%s",buffer);
}
if (strcmp(minans,ans[paths[2].a])>0) strcpy(minans,ans[paths[2].a]);
}
void dfs(int dep){
if (dep>=3){
int i;
for(i=0;i<4;i++) if (shu[i]>0) break;
if (i<4&&shu[i]==24){
output();
}
}else for(int i=0;i<4;i++)
for(int j=0;j<4;j++){
if (i!=j&&shu[i]>0&&shu[j]>0){
int t1=shu[i],t2=shu[j];
/* + 运算 */
shu[i]=shu[i]+shu[j];
shu[j]=0;
paths[dep]={i,j,'+'};
dfs(dep+1);
shu[i]=t1;shu[j]=t2;
/* - 运算 */
shu[i]=shu[i]-shu[j];
shu[j]=0;
paths[dep]={i,j,'-'};
dfs(dep+1);
shu[i]=t1;shu[j]=t2;
/* * 运算 */
shu[i]=shu[i]*shu[j];
shu[j]=0;
paths[dep]={i,j,'*'};
dfs(dep+1);
shu[i]=t1;shu[j]=t2;
/* / 运算 */
if ((shu[i] % shu[j]==0)&&shu[i]>=shu[j]) {
shu[i]=shu[i]/shu[j];
shu[j]=0;
paths[dep]={i,j,'/'};
dfs(dep+1);
shu[i]=t1;shu[j]=t2;
}
/* & 运算 */
shu[i]=shu[i]&shu[j];
shu[j]=0;
paths[dep]={i,j,'&'};
dfs(dep+1);
shu[i]=t1;shu[j]=t2;
/* | 运算 */
shu[i]=shu[i]|shu[j];
shu[j]=0;
paths[dep]={i,j,'|'};
dfs(dep+1);
shu[i]=t1;shu[j]=t2;
/* ^ 运算 */
shu[i]=shu[i]^shu[j];
shu[j]=0;
paths[dep]={i,j,'^'};
dfs(dep+1);
shu[i]=t1;shu[j]=t2;
}
}
}
int main(){
for(int i=0;i<4;i++) {
fin>>chars[i];
switch (chars[i])
{
case 'A':
shu[i]=1;
break;
case 'T':
shu[i]=10;
break;
case 'J':
shu[i]=11;
break;
case 'Q':
shu[i]=12;
break;
case 'K':
shu[i]=13;
break;
default:
shu[i]=chars[i]-'0';
break;
}
}
dfs(0);
fout<<minans;
}
没有合适的资源?快使用搜索试试~ 我知道了~
ACM/NOI/CSP比赛经验分享&代码程序资源:深度优先搜索
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共5个文件
cpp:5个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
需积分: 5 0 下载量 160 浏览量
2024-01-27
11:41:01
上传
评论
收藏 3KB ZIP 举报
温馨提示
ACM/NOI/CSP比赛经验分享&代码程序资源:深度优先搜索 全国青少年信息学奥林匹克联赛(National Olympiad in Informatics in Provinces,简称NOIP)自1995年至2020年已举办25次。每年由中国计算机学会统一组织。 NOIP在同一时间、不同地点以各省市为单位由特派员组织。全国统一大纲、统一试卷。初、高中或其他中等专业学校的学生可报名参加联赛。联赛分初赛和复赛两个阶段。初赛考察通用和实用的计算机科学知识,以笔试形式进行。复赛为程序设计,须在计算机上调试完成。参加初赛者须达到一定分数线后才有资格参加复赛。联赛分普及组和提高组两个组别,难度不同,分别面向初中和高中阶段的学生。
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 5 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/848cc228281a4a79b765db01f0843312_weixin_44026026.jpg!1)
rgb2gray
- 粉丝: 2w+
- 资源: 146
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)