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层,不 ...
随机推荐
- Linux定义系统提示符的变量:PS1
- Struct初学的,页面跳转
Filter控制器 jsp页面代码 <form action="page_login.action" method="post"> user ...
- Android --Vibrator--震动服务
1.取得震动服务的句柄 vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);或者vibrator = (Vibrator)getAppli ...
- C-整数划分
将正整数 n 表示成一系列正整数之和, n=n1+n2+…+nk, 其中 n1>=n2>=…>=nk>=1 , k>=1 . 正整数 n 的这种表示称为正整数 n 的划分 ...
- VMware Workstation 9.0 安装苹果Mac OS X10.9系统
摘自:http://www.wuwenhui.cn/3133.html 一.安装所需要的软件: 1.VMware Workstation 9.0 点击下载 2.unlock-all-v110.zip ...
- 08-UIKit(UITableTableViewCell、自定义Cell、xcode调试)
目录: 1. UITableTableViewCell 2. tag技术 3. 自定义Cell 4. 用nib文件构造自定义的静态表 5. TableView数据模型总结 6. Xcode代码调试 & ...
- 给VS自动添加注释
找到类文件所在路径:C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplatesCache\CSharp\ ...
- php 上传文件代码
通过 PHP,能够把文件上传到server.里面加入一些图片的推断,假设不加推断文件的类型就能够上传随意格式的文件. 为了站点的安全,肯定不让上传php文件,假设有人进入你的后台,上传了一个php文件 ...
- windows下apache如何完整卸载?
1.运行services.msc,在服务中停止 apache 服务.2.运行命令行程序,输入 sc delete apache,删除该服务3.删除apache文件夹.
- APEC计划指引我们前进:云计算服务将上升
APEC纲领指引我们前进:云计算服务业必将兴起 这是APEC领导人的合影: 这次APEC会议通过了<北京纲领>和<亚太伙伴关系声明>,进一步明白了亚太地区经济合作的发展方向.目 ...