hdu1427之速算24点
速算24点
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2430 Accepted Submission(s): 582
3 3 8 8
No
有这几种情况:1.(a@b)@(c@d),(c@d)@(a@b),(b@a)@(c@d),... 综合为(x1@y1)@(x1@y1)这种情况,不过得到的值可以为-24
2.((a@b)@c)@d,a@((b@c)@d),(d@(a@b)@c),...综合为((x1@y1)@x2)@y2这种情况,不过得到的值可以为-24
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=5;
char s[3];
int number[MAX]; void check(char ch,int &num){
if(ch == 'A')num=1;
else if(ch == 'J')num=11;
else if(ch == 'Q')num=12;
else if(ch == 'K')num=13;
else for(int i=2;i<10;++i){
if(ch == i+'0'){num=i;break;}
}
if(ch == '1')num=10;
} int f(int a,int op,int c){
if(op == 0)return a+c;
if(op == 1)return a-c;
if(op == 2)return a*c;
if(c && a%c == 0)return a/c;
return INF;
} bool calculate(int i,int j,int k){
int temp1,temp2;
temp1=f(number[0],i,number[1]);
if(temp1 != INF)temp2=f(number[2],k,number[3]);
if(temp2 == INF)temp1=INF;
if(temp1 != INF)temp1=f(temp1,j,temp2);
if(temp1 == 24 || temp1 == -24)return true;
temp1=f(number[0],i,number[1]);
if(temp1 != INF)temp1=f(temp1,j,number[2]);
if(temp1 != INF)temp1=f(temp1,k,number[3]);
if(temp1 == 24 || temp1 == -24)return true;
return false;
} int main(){
while(~scanf("%s",s)){
check(s[0],number[0]);
for(int i=1;i<=3;++i){
scanf("%s",s);
check(s[0],number[i]);
}
sort(number,number+4);
bool flag=false;
do{
for(int i=0;i<4 && !flag;++i)for(int j=0;j<4 && !flag;++j)for(int k=0;k<4 && !flag;++k){
flag=calculate(i,j,k);
//if(flag)cout<<number[0]<<' '<<number[1]<<' '<<number[2]<<' '<<number[3]<<endl;
//if(flag)cout<<i<<' '<<j<<' '<<k<<endl;
}
}while(!flag && next_permutation(number,number+4));
if(flag)printf("Yes\n");
else printf("No\n");
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=5;
char s[3];
int number[MAX];
bool flag; void check(char ch,int &num){
if(ch == 'A')num=1;
else if(ch == 'J')num=11;
else if(ch == 'Q')num=12;
else if(ch == 'K')num=13;
else for(int i=2;i<10;++i){
if(ch == i+'0'){num=i;break;}
}
if(ch == '1')num=10;
} int f(int a,int op,int c){
if(op == 0)return a+c;
if(op == 1)return a-c;
if(op == 2)return a*c;
if(c && a%c == 0)return a/c;
return INF;
} bool calculate(int i,int j,int k){
int temp1,temp2;
temp1=f(number[0],i,number[1]);
if(temp1 != INF)temp2=f(number[2],k,number[3]);
if(temp2 == INF)temp1=INF;
if(temp1 != INF)temp1=f(temp1,j,temp2);
if(temp1 == 24 || temp1 == -24)return true;
temp1=f(number[0],i,number[1]);
if(temp1 != INF)temp1=f(temp1,j,number[2]);
if(temp1 != INF)temp1=f(temp1,k,number[3]);
if(temp1 == 24 || temp1 == -24)return true;
return false;
} void Perm(int k){
if(k == 3){
for(int i=0;i<4 && !flag;++i)for(int j=0;j<4 && !flag;++j)for(int k=0;k<4 && !flag;++k){
flag=calculate(i,j,k);
}
return;
}
Perm(k+1);
for(int i=k+1;i<4;++i){
if(flag)return;
if(number[i] == number[k])continue;
swap(number[k],number[i]);
Perm(k+1);
swap(number[k],number[i]);
}
} int main(){
while(~scanf("%s",s)){
check(s[0],number[0]);
for(int i=1;i<=3;++i){
scanf("%s",s);
check(s[0],number[i]);
}
flag=false;
Perm(0);
if(flag)printf("Yes\n");
else printf("No\n");
}
return 0;
}
hdu1427之速算24点的更多相关文章
- Hdu1427 速算24点 2017-01-18 17:26 46人阅读 评论(0) 收藏
速算24点 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submiss ...
- hdu 1427 速算24点
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1427 速算24点 Description 速算24点相信绝大多数人都玩过.就是随机给你四张牌,包括A( ...
- 24点游戏&&速算24点(dfs)
24点游戏 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Sta ...
- hdu 1427 速算24点 dfs暴力搜索
速算24点 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem De ...
- HDU 1427 速算24点 (深搜)
题目链接 Problem Description 速算24点相信绝大多数人都玩过.就是随机给你四张牌,包括A(1),2,3,4,5,6,7,8,9,10,J(11),Q(12),K(13).要求只用' ...
- HDU 1427 速算24点【数值型DFS】
速算24点 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu 1427 速算24点【暴力枚举】
速算24点 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu1427 速算24点
</pre><pre> //#pragma comment(linker, "/STACK:102400000,102400000") //HEAD #in ...
- HDOJ 1427(dfs) 速算24点
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1427 思路分析: 题目要求判断是否存在一种运算组合使得4个数的计算结果为24,因为搜索的层次为3层,不 ...
随机推荐
- 删除: warning C4996: 'sprintf': This function or variable may be unsafe. Consider 方法
可以使用的最简单的方法: 选项Project | Configuration Properties | C/C++ | Preprocessor | Preproc ...
- mysql 参数optimizer_switch
mysql 5.1中开始引入optimizer_switch, 控制mysql优化器行为.他有一些结果集,通过on和off控制开启和关闭优化器行为.使用有效期全局和会话两个级别,在5.5中optimi ...
- iOS开发之XMPP即时通讯简单实现
首先搭载服务器和数据库 搭载服务器我用的是openfire,数据库用的是mysql 这里推荐两个链接 配置mysql,用的是mysql workbench http://justsee.iteye.c ...
- @(报错)could not find the main class, Program will exit(已解决)
原文 @(报错)could not find the main class, Program will exit(已解决) (很抱歉,如果你希望能更加清楚地看清图片或是图上的文字的话,你可以 ...
- img标签中的src为php文件
先来看个例子: <img src="imgcode.php" alt="看不清楚,换一张" style="cursor: pointer;&qu ...
- 获取证书以用于 Windows Azure 网站 (WAWS)
编辑人员注释:本文章由 Windows Azure 网站团队的项目经理 Erez Benari 撰写. 近年来,随着网络犯罪的上升,使用 SSL 保护网站逐渐成为一项备受追捧的功能,Windows A ...
- CString Format 乱码问题
CString m_buf;CStatic *m_static;char *szName;...m_buf.Format(":%s",szName);m_static->Se ...
- 使用ffmpeg视频编码过程中踩的一个坑
今天说说使用ffmpeg在写视频编码程序中踩的一个坑,这个坑让我花了好多时间,回头想想,非常多时候一旦思维定势真的挺难突破的.以下是不对的编码结果: ...
- Swift - 使用HTML5进行iOS开发(将HTML5打包成iOS应用)
最近越来越流行使用HTML5进行跨平台应用开发,先不说运行效率如何.从人力成本来说,只要写一套html页面就可以打包发布到安卓和iOS等多个平台,确实会省下不少时间和人力(这个领导最喜欢了). 下面简 ...
- AsyncTask的用法总结
这几天被AsyncTask虐得不行,在此总结下 首先: AsyncTask的参数介绍 在开发Android移动客户端的时候往往要使用多线程来进行操作,我们通常会将耗时的操作放在单独的线程执行,避免其占 ...