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 ...
随机推荐
- C++例题练习(2)
环境:Dev-C++( Version:5.6.1) 1.循环输入一个1-1000的整数,判断是否为素数(输入1时程序结束) 素数:只能被1和自身整除. 实现代码: #include <iost ...
- 【html】【21】高级篇--搜索框
下载: http://www.xwcms.net/js/bddm/25368.html 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...
- 一套帮助你理解C语言的测试题(转)
前言 原文链接:http://www.nowamagic.net/librarys/veda/detail/775 内容 在这个网站(http://stevenkobes.com/ctest.html ...
- PHP5.3后在本机运行很慢的解决方法
方法一:这是因为PHP 5.3在面对数据库配置信息中的“localhost”会犹豫,因此直接把这个地址改名为“127.0.0.1”,这个IP是IPv4下面的本地网络地址,实际作用和“localhost ...
- inline-block的兼容性问题
inline-block的兼容性问题 Inline-block是元素display属性的一个值.这个名字的由来是因为,display设置这个值的元素,兼具行内元素( inline elements)跟 ...
- MongoDB入门三步曲3--部署技术:主备、副本集和数据分片
mongodb部署--主备.副本及数据分片 主备复制 副本集 数据分片 主备复制 主备复制是最基本的一种多点部署方案,在读写分离.热备份.数据恢复等方面具有重要作用. 在真实的生产环境,主备库肯定需要 ...
- python学习笔记——列表生成式与生成器
1.列表生成式(List Comprehensions) python中,列表生成式是用来创建列表的,相较于用循环实现更为简洁.举个例子,生成[1*1, 2*2, ... , 10*10],循环用三行 ...
- Python获取两个ip之间的所有ip
int_ip = lambda x: '.'.join([str(x/(256**i)%256) for i in range(3,-1,-1)]) ip_int = lambda x:sum([25 ...
- Binding的源和路径
书上写着:Binding的源也就是数据的源头.Binding对于源的要求很简单-只要他是一个对象!并且通过属性(Property)公开自己的数据,它就可以作为Binding的源了.就像上一篇我写的那个 ...
- Codeforces Round #344 (Div. 2) C. Report
Report 题意:给长度为n的序列,操作次数为m:n and m (1 ≤ n, m ≤ 200 000) ,操作分为t r,当t = 1时表示将[1,r]序列按非递减排序,t = 2时表示将序列[ ...