UVa 10079 Pizza Cutting (water ver.)

本文介绍了一个简单的算法问题——如何通过给定数量的直线切割将比萨分割成尽可能多的块。该问题源自一个有趣的背景故事,但核心在于解决数学挑战。文章提供了完整的C++代码实现,用于计算任意数量的切割能够产生的最大比萨块数。

10079 - Pizza Cutting

Time limit: 8.333 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1020

When someone calls Ivan lazy, he claims that it is his intelligence that helps him to be so. If his intelligence allows him to do something at less physical effort, why should he exert more? He also claims 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 it among his friends. (Size of the pieces may not be the same. In fact, his piece will be larger than the others.) He thought a bit, and came to the conclusion that he can cut it into seven pieces by only three straight cuts through the pizza with a pizza knife. Accordingly, he cut the pizza in the following way (guess which one is Ivan's piece):

                                              

 

One of his friends, who never believed in Ivan’s smartness, was startled at 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 criticize him for stealing his idea) job with his computer. He wrote a program that took the number of straight cuts one makes through the pizza, and output a number representing the maximum 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 for doing the same job he did.

 

Input

The input file will contain a single integer N (0 <= N <= 210000000)  in each line representing the number of 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 should contain only one output integer without any leading or trailing space.

 

Sample Input:

5
10
-100

 

Sample Output:

16
56


完整代码:

/*0.012s*/

#include<cstdio>

int main(void)
{
	long long n;
	while (scanf("%lld", &n), n >= 0)
		printf("%lld\n", ((n * n + n) >> 1) + 1);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值