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 < )
{
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的更多相关文章
- PHP 中使用explode()函数切割字符串为数组
explode()函数的作用:使用一个字符串分割另一个字符串,打散为数组. 例如: 字符串 $pizza = "第1 第2 第3 第4 第5 第6"; 根据空格分割后:$piece ...
- Codeforces 842B Gleb And Pizza【几何,水】
B. Gleb And Pizza time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- Codeforces Round #430 B. Gleb And Pizza
Gleb ordered pizza home. When the courier delivered the pizza, he was very upset, because several pi ...
- Xtreme9.0 - Mr. Pippo's Pizza 数学
Mr. Pippo's Pizza 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/mr-pipp ...
- 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 ...
- CodeForces:#448 div2 a Pizza Separation
传送门:http://codeforces.com/contest/895/problem/A A. Pizza Separation time limit per test1 second memo ...
- Codeforces 895.A Pizza Separation
A. Pizza Separation time limit per test 1 second memory limit per test 256 megabytes input standard ...
- #448 div2 a Pizza Separation
A. Pizza Separation time limit per test1 second memory limit per test256 megabytes inputstandard inp ...
- Saddest's polar bear Pizza offered new YorkShire home
Saddest:adj,可悲的,悲哀的,polar,两级的,极地额,YorkShire,约克郡 A UK wildlife park has confirmed that it is offering ...
随机推荐
- is not in the sudoers file 解决(转)
解决方案:首需要切换到root身份$su -(注意有- ,这和su是不同的,在用命令"su"的时候只是切换到root,但没有把root的环境变量传过去,还是当前用户的环境变量,用& ...
- Eclipse配置CAS client
1.新建一个Maven项目 2.Next,选择 3.输入group id 和 artifact id --> Finish 4.项目创建完成的目录结构 编辑pom.xml文件,写上依赖 注意把 ...
- ajax查询数据返回结果不变
在使用流水号的时候,Google浏览器没有问题,但是IE有缓存,如果ajax请求的参数没有变化,那么就会返回缓存里的数据 解决方法:ajax请求的时候传值的参数设置一个时间戳就OK了(没什么特别意义, ...
- 【仿携程JQuery日期价格表】
今天比较闲所以就花点时间又写了点东西. 相信这种价格表大家不会陌生 现在我就模仿它做一个简单版本的.效果如下 首先需要两个时间控件,我这里用的是HTML5里面的时间控件,这个没限制喜欢用什么就用什么 ...
- PHP学习笔记(3) - 奇怪的class与autoload
PHP的class与其他语言有很多不同点.PHP允许很奇葩的在静态方法中调用实例方法,提供了关键字self和static用于访问类自身的静态成员.self永远是指当前的类,而static则可能会变成指 ...
- PHP LINUX Notice: undefined $_GET完美解决方法
PHP Notice: undefined 平时用$_GET[‘xx’] 取得参数值时,如果之前不加判断在未传进参数时会出现这样的警告: PHP Notice: undefined index xxx ...
- CentOS 编译安装Apache2.4.10
1.准备编译环境 yum -y install gcc make cmake autoconf libtool libevent 安装apache必须的依赖包 yum -y install apr-u ...
- Python设计模式——状体模式
需求,根据当前的时间,返回工作状态 #encoding=utf-8 __author__ = 'kevinlu1010@qq.com' def get_state(hour): if hour> ...
- Highcharts资料
对应的API: http://api.hcharts.cn/#chart.events 对应的中文网实例:http://www.hcharts.cn/demo/highcharts/dynamic ...
- SQL 中With as 的用法
转自:http://www.cnblogs.com/superyinhai/archive/2010/04/09/1708643.html 一.WITH AS的含义 WITHAS短语,也叫做子查询部分 ...