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 < )
{
return -;
}
else if (cut == )
{
return ;
}
else
{
return maxPizza(cut - ) + cut;
}
}
}

其他人的解法:

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

return cut <  ? - : Enumerable.Range(, cut).Aggregate(, (x, y) => x + y);
public class Pizza
{ public static int maxPizza(int cut) { if(cut < )
{
return -;
} if(cut == )
{
return ;
} int result = ; for(int i = ; i <= cut; i++)
{
result = result + i;
} return result + ;
}
}

Pizza pieces的更多相关文章

  1. PHP 中使用explode()函数切割字符串为数组

    explode()函数的作用:使用一个字符串分割另一个字符串,打散为数组. 例如: 字符串 $pizza = "第1 第2 第3 第4 第5 第6"; 根据空格分割后:$piece ...

  2. Codeforces 842B Gleb And Pizza【几何,水】

    B. Gleb And Pizza time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  3. Codeforces Round #430 B. Gleb And Pizza

    Gleb ordered pizza home. When the courier delivered the pizza, he was very upset, because several pi ...

  4. Xtreme9.0 - Mr. Pippo's Pizza 数学

    Mr. Pippo's Pizza 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/mr-pipp ...

  5. Codeforces Round #448 (Div. 2) A. Pizza Separation【前缀和/枚举/将圆(披萨)分为连续的两块使其差最小】

    A. Pizza Separation time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. CodeForces:#448 div2 a Pizza Separation

    传送门:http://codeforces.com/contest/895/problem/A A. Pizza Separation time limit per test1 second memo ...

  7. Codeforces 895.A Pizza Separation

    A. Pizza Separation time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. #448 div2 a Pizza Separation

    A. Pizza Separation time limit per test1 second memory limit per test256 megabytes inputstandard inp ...

  9. Saddest's polar bear Pizza offered new YorkShire home

    Saddest:adj,可悲的,悲哀的,polar,两级的,极地额,YorkShire,约克郡 A UK wildlife park has confirmed that it is offering ...

随机推荐

  1. Ext.Net学习笔记16:Ext.Net GridPanel 折叠/展开行

    Ext.Net学习笔记16:Ext.Net GridPanel 折叠/展开行 Ext.Net GridPanel的行支持折叠/展开功能,这个功能个人觉得还说很有用处的,尤其是数据中包含图片等内容的时候 ...

  2. 用 CSS 隐藏页面元素的 5 种方法

    原文链接:用 CSS 隐藏页面元素的 5 种方法,转载请注明来源! 用 CSS 隐藏页面元素有许多种方法.你可以将 opacity 设为 0.将 visibility 设为 hidden.将 disp ...

  3. 生产者消费者问题c语言实现

    #include <stdio.h> #include <process.h> #include <Windows.h> //信号量与关键段 CRITICAL_SE ...

  4. [java学习笔记]Hello World那些事

    我们安装和配置好java后,必须得大展拳脚一番,根据国际惯例,第一个程序必须是Hello World,下面我们就看看Hello World的那些事. 1.Hello World的运行 Hello Wo ...

  5. cocos2dx 初探 - VS2012 HELLOWORLD

    最近决定用cocos2dx 来做些试验性的东西,先装了个vs2012 再从网上下了cocos2dx-2.1.4就开工了. 仅是Windows 桌面调试还是很简单的. 上面三个项目源: Hellocpp ...

  6. 最近用到的Linux常用命令总结

    最近用到的Linux常用命令总结 - ls :显示当前目录文件信息 `ls -a -l` - cd :目录跳转 cd .. 上级目录 cd ~ home目录 cd - 最近目录 - cat :在屏幕上 ...

  7. vmware RHEL6.x 开启FTP和TELNET服务--root权限

    //vmware RHEL6.x默认未安装ftp工具,需自己安装--root权限 第一部分:ftp //检查ftp是否安装 # rpm -qa | grep -i vsftpd //找到ftp的rpm ...

  8. 构造SEH来实现跳转-转载

    下面的代码出自CSDN Delphi版的一高人(kiboisme 蓝色光芒) procedure ExceptProc{ExceptionRecord,SEH,Context,DispatcherCo ...

  9. Python设计模式——模版方法模式

    1.模版方法模式 做题的列子: 需求:有两个学生,要回答问题,写出自己的答案 #encoding=utf-8 __author__ = 'kevinlu1010@qq.com' class Stude ...

  10. Java学习-数组

    1.数组的是Object的直接子类,它属于“第一类对象”,但是它又与普通的java对象存在很大的不同,类名为:[I 一维数组:[I 二维数组:[[I 三维数组:[[[I 2.[代表了数组的维度,一个[ ...