折线分割平面 (hdu 2050)

我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线分割平面的最大数目。比如,一条折线可以将平面分成两部分,两条折线最多可以将平面分成7部分,具体如下所示。 

Input

输入数据的第一行是一个整数C,表示测试实例的个数,然后是C 行数据,每行包含一个整数n(0<n<=10000),表示折线的数量。 
 

Output

对于每个测试实例,请输出平面的最大分割数,每个实例的输出占一行。 
 

Sample Input

2
1
2

Sample Output

2
7

题解:做这道题首先你要画几组样列,你会发现第n条直线最多与n-1条直线相交,将n-1个平面分为2*(n-1)个,那么一条折线时两条射线(相当于两条直线),可以分出2*2*(n-1)个平面,两射线交点角处还多了一个面,所以第n个图形比上一个多了4*(n-1)+1个面。

代码如下:

#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <ctime>
#define maxn 10007
#define N 107
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.000000001
using namespace std;
typedef long long ll;
long long int dp[10011];
int main()
{
    dp[1]=2;dp[2]=7;
    for(int i=3; i<=10011; i++)
        dp[i]=dp[i-1]+4*(i-1)+1;
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        cout<<dp[n]<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/baiyi_destroyer/article/details/81099948