标签:blog http io os for sp 2014 log amp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1466
思路:我们知道n条直线得到的最多交点数为(n-1)n/2,据此可以推测数组的大小。而如果n条直线的交点个数<n(n-1)/2,则说明有直线和直线平行,这种情况下,我们设r条直线互不平行,假如r条直线有j个交点成立,有n-r条平行直线分别和r条互不平行直线相交,新增交点n-r个,n条直线得到交点个数为j+(n-r)*r
#include <iostrea
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 30
#define MAXM 200
using namespace std;
bool f[MAXN][MAXM]; //f[i][j]=true表明i条线段j个交点的方案存在
void getF()
{
for(int i=0;i<=20;i++)
for(int j=0;j<=191;j++)
{
if(j==0) f[i][j]=true; //i条线段无交点,即i条线段互相平行,该情况存在
}
for(int i=0;i<=20;i++)
for(int r=0;r<i;r++) //枚举互不平行的直线数r
for(int j=0;j<=191;j++)
{
if(f[r][j])
f[i][(i-r)*r+j]=true;
}
}
int main()
{
getF();
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n*(n-1)/2;i++)
if(f[n][i])
printf("%d ",i);
printf("%d\n",n*(n-1)/2);
}
return 0;
}
标签:blog http io os for sp 2014 log amp
原文地址:http://blog.csdn.net/qpswwww/article/details/40863805