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层,不 ...
随机推荐
- debian支持ll命令
debian支持ll命令 $ ll -bash: ll: command not found 没有ll这个命令.尽管也知道ll事实上 是ls -l 这个命令的别名,可是总感觉不是非常习惯.由于之前一直 ...
- App状态管理-AppDelegate
前面提到AppDelegate的实例在UIApplicationMain函数中被创建并调用. AppDelegate的主要作用,可以从其继承类可以看出 class AppDelegate:UIResp ...
- 1688: [Usaco2005 Open]Disease Manangement 疾病管理( 枚举 )
我一开始写了个状压dp..然后没有滚动就MLE了... 其实这道题直接暴力就行了... 2^15枚举每个状态, 然后检查每头牛是否能被选中, 这样是O( 2^15*1000 ), 也是和dp一样的时间 ...
- Jsp分页实例---真分页
网页的分页功能的实现比较简单,实现方法也多种多样. 今天总结一个简单的Jsp真分页实例. 首先,提到分页就要先明确一个概念,何为真分页何谓假分页. 假分页:一次性从数据库读出表的所有数据一次性的返回给 ...
- java字符操作获取汉字的拼音以及其它经常使用工具
公司需求年年有,今年有点小特殊,哈哈. 忽然加了个需求,说要实现汉字转拼音查询. 在努力下写出来了,如今分享一下吧!.! /** * 汉字转拼音缩写 * * @param str * 要转换的汉字字符 ...
- Bernstein polynomials
Bernstein多项式能够用来一致逼近闭区间上的连续函数. 对于[0,1]上的连续函数f(x),定义Bernstein多项式 B_n(f,x) = sum{k=0..n} f(k/n)C(k,n)t ...
- HDU Wolf and Rabbit
Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit must h ...
- Docker学习笔记(1) — docker 常用命令
1. docker version显示 Docker 版本信息.2. docker info显示 Docker 系统信息,包括镜像和容器数.3. docker searchdocker search ...
- 关于Linux路由表的route命令(转)
查看 Linux 内核路由表 使用下面的 route 命令可以查看 Linux 内核路由表. # route Destination Gateway Genmask Fl ...
- 【Android开源项目分析】android轻量级开源缓存框架——ASimpleCache(ACache)源代码分析
转载请注明出处:http://blog.csdn.net/zhoubin1992/article/details/46379055 ASimpleCache框架源代码链接 https://github ...