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. angularJs中ui-router的使用

    学习使用angular中,ui-route是其中的一个难点,简单使用没什么问题,但涉及到多级嵌套,就感觉有茫然,查了很多资料,踩过很多坑,到目前为止也不能说对ui-route有全面了解:这里只是把填补 ...

  2. Java实战之04JavaWeb-01Servlet

    一.Http协议 1.什么是http协议? http协议就是描述客户端与服务器端交互过程的 2.http的请求 3.http的响应 二.Servlet的简介 1.Servlet的概述 Servlet: ...

  3. Mysql多实例 安装以及配置

    MySQL多实例 1.什么是MySQL多实例 简单地说,Mysql多实例就是在一台服务器上同时开启多个不同的服务端口(3306.3307),同时运行多个Mysql服务进程,这些服务进程通过不同的soc ...

  4. python 数据类型(列表)学习笔记

    列表 创建列表: name_list = ['alex', 'seven', 'eric'] 或 name_list = list(['alex', 'seven', 'eric']) 其实今天学习的 ...

  5. Python设计模式——装饰模式(Decorator)

    假如我们需要开发一个程序来展示一个人穿衣服的过程. #encoding=utf-8 __author__ = 'kevinlu1010@qq.com' class Person(): def __in ...

  6. hdu 1695 GCD 莫比乌斯反演入门

    GCD 题意:输入5个数a,b,c,d,k;(a = c = 1, 0 < b,d,k <= 100000);问有多少对a <= p <= b, c <= q <= ...

  7. 我的PHP之旅--SQL语句

    SQL语句 结构化查询语言(Structured Query Language)简称SQL,是一种操作数据的语言. 增加记录 INSERT INTO table_name(字段1, 字段2, 字段3) ...

  8. Java注释模板

    /**   *    * 项目名称:${project_name}   * 类名称:${type_name}   * 类描述:   * 创建人:${user}   * 创建时间:${date} ${t ...

  9. visualvm 监控 远程 机器上的 Java 程序

    JDK里面本身就带了很多的监控工具,如JConsole等. 我们今天要讲的这款工具visualvm,就是其中的一款.但是这款工具是在JDK1.6.07及以上才有的.它能够对JAVA程序的JVM堆.线程 ...

  10. shell 练习

    shell 练习 iii= ] do iii=$[$iii+] echo -n "$iii " done iii= ] do iii=$[$iii+] echo -n " ...