[华为机试练习题]44.24点游戏算法
来源:互联网 发布:自定义oid取不到数据 编辑:程序博客网 时间:2024/06/06 03:06
题目
注意: 6 + 2 * 4 + 10 = 24 不是一个数字一个数字的计算
代码
/*---------------------------------------* 日期:2015-07-03* 作者:SJF0115* 题目:24点游戏算法* 来源:华为机试练习题-----------------------------------------*/#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;// 迭代 模拟各种组合void helper(vector<int> &num,int index,int result,bool &isSucess){ if(index == 4){ if(result == 24){ isSucess = true; }//if return; }//if for(int i = 0;i < 4;++i){ if(i == 0){ //op.push_back('+'); helper(num,index+1,result+num[index],isSucess); }//if else if(i == 1){ //op.push_back('-'); helper(num,index+1,result-num[index],isSucess); }//else else if(i == 2){ //op.push_back('*'); helper(num,index+1,result*num[index],isSucess); }//else else{ //op.push_back('/'); helper(num,index+1,result/num[index],isSucess); }//else //op.pop_back(); // 找到解决方案 if(isSucess){ break; }//if }//for}bool Game24Points(int a, int b, int c, int d){ vector<int> num; num.push_back(a); num.push_back(b); num.push_back(c); num.push_back(d); bool isSucess = false; helper(num,1,num[0],isSucess); if(isSucess){ return isSucess; }//if // 全排列 主要应对 6 + 2 * 4 + 10 = 24 while(next_permutation(num.begin(),num.end())){ helper(num,1,num[0],isSucess); if(isSucess){ return isSucess; }//if }//while return isSucess;}
2 0
- [华为机试练习题]44.24点游戏算法
- [华为机试练习题]25.圆桌游戏
- 华为OJ平台练习题--24点游戏
- 华为oj_24点游戏算法
- 华为OJ24点游戏算法
- 华为机试—24点游戏
- 华为机试:计算字符串的距离、24点游戏算法
- 华为 2033 24点游戏算法
- 华为oj 24点游戏算法
- 【华为OJ】【097-24点游戏算法】
- 华为OJ 初级:24点游戏算法
- 24点游戏算法-华为OJ
- 华为OJ:24点游戏算法
- 华为oj初级 24点游戏算法
- 华为机试练习题汇总
- 华为机试在线练习题
- 华为机试题:24点游戏
- 华为OJ——24点游戏算法
- impdp时卡住,DW等待library cache lock
- 非常不错的一款打字代码效果
- [iOS]使用autolayout的时候会有明明设置和父视图左右间距为0但却还有空隙问题
- leetcode#224 Basic Calculator
- HTML5*与html
- [华为机试练习题]44.24点游戏算法
- 黑马程序员——Java基础---线程的另一个总结(4)--线程通信(互斥),线程范围的共享变量
- hdu 1757 A Simple Math Problem(矩阵连乘)
- ACPI 高级配置与电源接口 (介绍的比较详细)
- 拾匦尚世霉壮敬染由冠槐文
- Codeforces Round #310 (Div. 1) B. Case of Fugitive
- 20150630_UI之按钮UIButton使用
- C++实现顺序栈之两栈共享存储空间
- ViewController 生命周期重要方法