您的位置:首页 > 其它

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

然后就是套公式算啦,这道题还能安慰安慰我(哭

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm 算法 数论