递归

数字的题目,不是很难。

用了下递归处理这个问题。但是如果能看透,直接用数学的方案去解,简单太多了

We want to make a row of bricks that is goal inches long. We have a number of small bricks (1 inch each) and big bricks (5 inches each). Return true if it is possible to make the goal by choosing from the given bricks. This is a little harder than it looks and can be done without any loops. See also:Introduction to MakeBricks


makeBricks(3, 1, 8) → true
makeBricks(3, 1, 9) → false
makeBricks(3, 2, 10) → true

 

 

public boolean makeBricks(int small, int big, int goal) {
  int bigm=0;
     int smalln=0;
     bigm=goal/5;
     smalln=goal%5;
     int tran=1;
     if (bigm<=big&&smalln<=small){     
      return true;
      }
     
     if (bigm >big)
      tran=bigm-big;
      if (small-5*tran>=0)
     if( makeBricks (small-5*tran,big+tran,goal)) return true;
      return false;
}

 

---

public boolean makeBricks(int small, int big, int goal) {
 
     int M=goal/5;
     if (big>=M){
      if (goal%5<=small) return true;
     }
     else if(goal-5*big <=small)return true;
     return false;
}

posted @ 2016-05-09 01:51  deep-hope  阅读(98)  评论(0)    收藏  举报