題 意: 給一個 pizza,問切 n 刀,能夠使 pizza 分成最多份是幾份。
解法: 若 pizza 當下已經切了 k 刀,若要切第 k+1 刀,那這刀必定要經過前面的 k 刀,這樣 pizza 就能多分出 k 塊,則可以推出通式 p(n) = (n*(n+1))/2 + 1。
TAG: Math
注意: 答案會要使用 long long
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Tittle: 10079 - Pizza Cutting | |
* Author: Cheng-Shih, Wong | |
* Date: 2015/03/17 | |
* File: 10079 - Pizza Cutting.cpp - solve it | |
*/ | |
// include files | |
#include <iostream> | |
#include <cstdio> | |
#include <cstring> | |
using namespace std; | |
// definitions | |
#define FOR(i,a,b) for( int i=(a),_n=(b); i<=_n; ++i ) | |
#define clr(x,v) memset( x, v, sizeof(x) ) | |
typedef long long ll; | |
// declarations | |
ll n; | |
// functions | |
// main function | |
int main( void ) | |
{ | |
// input | |
while( scanf( "%lld", &n )==1 && n >= 0LL ) { | |
// solve & output | |
printf( "%lld\n", (n*(n+1LL))/2LL+1LL ); | |
} | |
return 0; | |
} |
沒有留言:
張貼留言
任何意見都樂意傾聽