码迷,mamicode.com
首页 > 其他好文 > 详细

递归-24点

时间:2020-07-06 19:27:38      阅读:42      评论:0      收藏:0      [点我收藏+]

标签:count   als   span   pac   lazy   return   math   info   col   

....又是以前虐过我的期末习题..哪里摔倒就哪里爬起来吧

技术图片

 

 

 1 #include<iostream>
 2 #include<math.h>
 3 #define  N 1e-3
 4 using namespace std;
 5 
 6 bool IsZero(double a){
 7     return abs(a) < N;
 8 }
 9 bool count24(double a[],int n)//递归传数得知道传入几个以及剩下的数的数组
10 {
11   if(IsZero(a[0]-24)) {
12 
13      return true;
14   }
15 if(n==1)
16     return false;
17 
18     double x,y;
19     double b[10];
20     int m=0;
21     for (int i = 0; i < n-1; ++i) {
22         for (int j = i+1; j < n; ++j) {
23             for (int k = 0; k < n; ++k) {
24                 if(k!=i&&k!=j)
25                 b[m++]=a[k];//顺便b有多少也得出来了
26 
27             }
28             //cout<<m;
29             b[m]=a[i]+a[j];
30             if(count24(b,m+1))
31                 return true;
32             b[m]=a[i]-a[j];
33             if(count24(b,m+1))
34                 return true;
35             b[m]=a[j]-a[i];
36             if(count24(b,m+1))
37                 return true;
38             b[m]=a[i]*a[j];
39             if(count24(b,m+1))
40                 return true;
41             //cout<<m;
42             if(!IsZero(a[i])){
43                 //cout<<"in";
44                 b[m]=a[j]/a[i];
45                 if(count24(b,m+1))
46 
47                     return true;
48             }
49             if(!IsZero(a[j])){
50                 //cout<<"s";
51                 b[m]=a[i]/a[j];
52                 if(count24(b,m+1))
53                     return true;
54             }
55 
56 
57             
58         }
59         
60     }
61     return false;//所有的循环都不行,才可以return false
62 }
63 int main(){
64     double a[10];
65     for (int i = 0; i < 4; ++i) {
66         cin>>a[i];
67         
68     }
69     if(count24(a,4))
70         cout<<"yes";
71     else
72         cout<<"no";
73 
74 
75 
76 }

又是感觉自己傻乎乎的一天.

把b[m]=a[i]?a[j]写成m++

还觉得没啥毛病,,,

关键是,他每次都加加,就不知能把m加到多少了呜呜呜

好多细节得注意呀呜呜呜

递归-24点

标签:count   als   span   pac   lazy   return   math   info   col   

原文地址:https://www.cnblogs.com/zhmlzhml/p/13256215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!