UVA, 10079 Pizza Cutting
2016-03-25 14:49
316 查看
这个学期选了大大的算法课,渣旧表示生无可恋。。。但是已经励志这个学期做学霸了,自己选的课,含着泪也要上下去QAQ
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 and tries to do some
work at less effort; this is not his laziness,rather this is his intellectual smartness.
Once Ivan was asked to cut a pizza into seven pieces to distribute itamong his friends. (Size of the pieces may not be the same. In fact, hispiece will be larger than the others.) He thought a bit, and came to theconclusion that he can cut it into seven
pieces by only three straight cutsthrough the pizza with a pizza knife. Accordingly, he cut the pizza in thefollowing way (guess which one is Ivan’s piece):
One of his friends, who never believed in Ivan’s smartness, was startledat this intelligence. He thought, if Ivan can do it, why can’t my computer?So he tried to do a similar (but not exactly as Ivan’s, for Ivan will criticizehim for stealing his idea) job
with his computer. He wrote a program thattook the number of straight cuts one makes through the pizza, and output a number representing themaximum number of pizza pieces it will produce.Your job here is to write a similar program. It is ensured that Ivan’s
friend won’t criticize you fordoing the same job he did.
Input
The input file will contain a single integer N (0 ≤ N ≤ 210000000) in each line representing the numberof straight line cuts one makes through the pizza. A negative number terminates the input.
Output
Output the maximum number of pizza pieces the given number of cuts can produce. Each line shouldcontain only one output integer without any leading or trailing space.
Sample Input
5
10
-100
Sample Output
16
56
··················································································································································································································
题目:用刀切皮萨,问n刀最多能切成几块
嗯。。。一点一点慢慢推吧
刀数 所切块数
f(0) 0 1
f(1) 1 1+2
f(2) 2 1+2+2 f(2)=f(2-1)+2
f(3) 3 1+2+2+3 f(3)=f(3-1)+3
f(4) 4 1+2+2+3+4
: :
f(n-1) n-1
f(n) n f(n)=f(n-1)+n
f(n+1) n+1 f(n+1)=f(n)+n+1
可得递推式 f(n)=n*(n+1)/2+1
然后就是套公式算啦,这道题还能安慰安慰我(哭
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 and tries to do some
work at less effort; this is not his laziness,rather this is his intellectual smartness.
Once Ivan was asked to cut a pizza into seven pieces to distribute itamong his friends. (Size of the pieces may not be the same. In fact, hispiece will be larger than the others.) He thought a bit, and came to theconclusion that he can cut it into seven
pieces by only three straight cutsthrough the pizza with a pizza knife. Accordingly, he cut the pizza in thefollowing way (guess which one is Ivan’s piece):
One of his friends, who never believed in Ivan’s smartness, was startledat this intelligence. He thought, if Ivan can do it, why can’t my computer?So he tried to do a similar (but not exactly as Ivan’s, for Ivan will criticizehim for stealing his idea) job
with his computer. He wrote a program thattook the number of straight cuts one makes through the pizza, and output a number representing themaximum number of pizza pieces it will produce.Your job here is to write a similar program. It is ensured that Ivan’s
friend won’t criticize you fordoing the same job he did.
Input
The input file will contain a single integer N (0 ≤ N ≤ 210000000) in each line representing the numberof straight line cuts one makes through the pizza. A negative number terminates the input.
Output
Output the maximum number of pizza pieces the given number of cuts can produce. Each line shouldcontain only one output integer without any leading or trailing space.
Sample Input
5
10
-100
Sample Output
16
56
··················································································································································································································
题目:用刀切皮萨,问n刀最多能切成几块
嗯。。。一点一点慢慢推吧
刀数 所切块数
f(0) 0 1
f(1) 1 1+2
f(2) 2 1+2+2 f(2)=f(2-1)+2
f(3) 3 1+2+2+3 f(3)=f(3-1)+3
f(4) 4 1+2+2+3+4
: :
f(n-1) n-1
f(n) n f(n)=f(n-1)+n
f(n+1) n+1 f(n+1)=f(n)+n+1
可得递推式 f(n)=n*(n+1)/2+1
然后就是套公式算啦,这道题还能安慰安慰我(哭
#include <iostream> #include<cstdio> using namespace std; int main() { long long N; long long F; while(scanf("%I64d",&N)&&N>=0) { F=(N+1)*N/2+1; printf("%d\n",&F); } return 0; }
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析