Pizza pieces

Pizza pieces

Description

In her trip to Italy, Elizabeth Gilbert made it her duty to eat perfect pizza. One day, she ordered one for dinner. And then some Italian friends appeared at her room.

The problem is that there were many people who ask for a piece of pizza at that moment. And she had a knife that only cuts straight.

Given a number K (K<=45000), help her get the maximum of pieces possible (not necessarily of equal size) with Kcuts. If K is a negative number, the result must be -1 (or Nothing in Haskell).

Examples

maxPizza(0) == 1
maxPizza(1) == 2
maxPizza(3) == 7

4刀,可以分成11块

是11块, 
这个数有规律: 
一刀也不切,切0刀:还是1快 
切1刀:1+1=2块 
切2刀:2+2=4 
切3刀:3+4=7 
切4刀:4+7=11 
切5刀:5+11=16 
当前下刀能切成的块数=现在是第几刀的数+前一刀已经切成的块数 
公式:Xn=(n+1)*n/2+1,Xn是切成的块数,n是切割的次数.

使用递归处理,需要注意使用尾递归

顾名思义,尾递归就是从最后开始计算, 每递归一次就算出相应的结果, 也就是说, 函数调用出现在调用者函数的尾部, 因为是尾部, 所以根本没有必要去保存任何局部变量. 直接让被调用的函数返回时越过调用者, 返回到调用者的调用者去。尾递归就是把当前的运算结果(或路径)放在参数里传给下层函数,深层函数所面对的不是越来越简单的问题,而是越来越复杂的问题,因为参数里带有前面若干步的运算路径。

  尾递归是极其重要的,不用尾递归,函数的堆栈耗用难以估量,需要保存很多中间函数的堆栈。比如f(n, sum) = f(n-1) + value(n) + sum; 会保存n个函数调用堆栈,而使用尾递归f(n, sum) = f(n-1, sum+value(n)); 这样则只保留后一个函数堆栈即可,之前的可优化删去。

public class Pizza
{

    public static int  maxPizza(int cut) {
        //TODO : Code goes here
      if (cut < 0)
            {
                return -1;
            }
            else if (cut == 0)
            {
                return 1;
            }
            else
            {
                return maxPizza(cut - 1) + cut;
            }
  }
}

其他人的解法:

使用for循环的,当然了这个for循环可以使用Linq来简化

return cut < 0 ? -1 : Enumerable.Range(1, cut).Aggregate(1, (x, y) => x + y);
public class Pizza
{

    public static int  maxPizza(int cut) {

    if(cut < 0)
    {
      return -1;
    }

    if(cut == 0)
    {
      return 1;
    }

    int result = 0;

    for(int i = 1; i <= cut; i++)
    {
      result = result + i;
    }

    return result + 1;
  }
}
时间: 2024-08-03 04:45:29

Pizza pieces的相关文章

Pizza Cutting

Pizza Cutting When someone calls Ivan lazy, he claims that it is his intelligence that helps him to be so. If hisintelligence allows him to do something at less physical effort, why should he exert more? He alsoclaims that he always uses his brain an

完美pizza,由我做主

参考:http://blog.sina.com.cn/s/blog_4a5089ff0100a6kd.html 制作过程:1.把制作pizza面饼的所有配料揉成面团,使劲揉,揉到面团变得十分劲道,抻开面团,面团能形成一张比较薄的薄膜(扩展阶段),就可以了.把面团放在26度左右的环境下,盖上保鲜膜,发酵到变成2倍大(夏天放在室温就可以).(关于面团的揉面发酵,请参考手工揉面发酵步骤图)2.面团发酵的时候,可以准备其他材料.把火腿.青椒切成丁,金枪鱼罐头滤去汁液.3.马苏里拉芝士刨成丝.4.约需要1

hdu 2368 Alfredo&#39;s Pizza Restaurant(简单数学题)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2368 题目很简单,但是比较恶心,用sqrt WA到死也不过,不用秒过: 忍不住吐槽一下; Problem Description Traditionally after the Local Contest, judges and contestants go to their favourite restaurant,

Saddest&#39;s polar bear Pizza offered new YorkShire home

Saddest:adj,可悲的,悲哀的,polar,两级的,极地额,YorkShire,约克郡 A UK wildlife park has confirmed that it is offering a new home to a lonely polar bear named Pizza who is living trapped in a chinese mall. 一个英国野生公园报告称他们为名为Pizz的一个孤独的北极熊提供了一个新家,它在中国生活陷入困难. A UK wildlife

Operating System: Three Easy Pieces --- Beyond Physical Memory: Mechanisms (Note)

Thus far, we have assumed that an address space is unrealistically small and fits into the physical memory. In fact, we have been assuming that every address space of ervery running process fits into memory. We will now relax these big assumptions, a

【暑假】[深入动态规划]UVa 1628 Pizza Delivery

UVa 1628 Pizza Delivery 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51189 思路:   本体与修缮长城一题有所相似.所以解法有相似之处. 不同之处就是本体可能会产生负情况,即送餐时间晚了客户会反过来找你要钱所以需要放弃,但修缮长城只有费用,顺手修了肯定是一个不错的选择. 依旧将区间两端与位置作为状态不过要添加一维cnt表示还需要送餐的人数.类似地定义:d[i][j][cnt][p]表示

hdu 4628 Pieces(状态压缩+记忆化搜索)

Pieces Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1811    Accepted Submission(s): 932 Problem Description You heart broke into pieces.My string broke into pieces.But you will recover one

UVA 10213 How Many Pieces of Land? 欧拉定理

欧拉定理 V-E+F=C+1 Problem G How Many Pieces of Land? Input: Standard Input Output: Standard Output Time Limit: 3 seconds   You are given an elliptical shaped land and you are asked to choose n arbitrary points on its boundary. Then you connect all these

HDU -4628 Pieces

http://acm.hdu.edu.cn/showproblem.php?pid=4628 Pieces Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1610    Accepted Submission(s): 850 Problem Description You heart broke into pieces.My str