zoukankan      html  css  js  c++  java
  • hdoj_1466计算直线的交点数

    计算直线的交点数

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 6625    Accepted Submission(s): 2921


    Problem Description
    平面上有n条直线,且无三线共点,问这些直线能有多少种不同交点数。
    比如,如果n=2,则可能的交点数量为0(平行)或者1(不平行)。
     

    Input
    输入数据包含多个测试实例,每个测试实例占一行,每行包含一个正整数n(n<=20),n表示直线的数量.
     

    Output
    每个测试实例对应一行输出,从小到大列出所有相交方案,其中每个数为可能的交点数,每行的整数之间用一个空格隔开。
     

    Sample Input
    2 3
     

    Sample Output
    0 1 0 2 3

    1、第四条与其余直线全部平行 => 0+4*0+0=0;

    2、第四条与其中两条平行,交点数为0+(n-1)*1+0=3;
    3、第四条与其中一条平行,这两条平行直线和另外两点直线的交点数为(n-2)*2=4,而另外两条直线既可能平行也可能相交,因此可能交点数为:
        0+(n-2)*2+0=4    或者  0+(n-2)*2+1=5     
    4、 第四条直线不与任何一条直线平行,交点数为:
        0+(n-3)*3+0=3  或0+ (n-3)*3+2=5  或0+ (n-3)*3+3=6

    即n=4时,有0个,3个,4个,5个,6个不同交点数。

    从上述n=4的分析过程中,我们发现:
    m条直线的交点方案数
    =(m-r)条平行线与r条直线交叉的交点数
     + r条直线本身的交点方案
    =(m-r)*r+r条之间本身的交点方案数(0<=r<m)

    #include <iostream>
    #include <cstring>
    #include <vector>
    #include <algorithm>
    using namespace std;
    #define MAX 21
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int n, i, j, k, x;
    	vector<int>array[MAX];
    	array[1].push_back(0);
    	array[2].push_back(0);
    	array[2].push_back(1);
    	for(i = 3; i < MAX; i++) //第i条直线
    	{
    		for(j = 0; j < i; j++)
    		{
    			if(j == 0)
    			{
    				array[i].push_back(0);
    			}
    			else
    			{
    				for(k = 0; k < array[j].size(); k++)
    				{
    					array[i].push_back((i - j) * j + array[j][k]);
    				}
    			}
    		}
    		sort(array[i].begin(), array[i].end());
    	}
    	while(cin >> n)
    	{
    		x = array[n][0];
    		cout << x << " ";
    		for(i = 1; i < array[n].size() - 1; i++)
    		{
    			if(x != array[n][i])
    			cout << array[n][i] << " ";
    			x = array[n][i];
    		}
    		if(x != array[n][array[n].size() - 1])
    		cout << array[n][i] << endl;
    	}
    	return 0;
    }


  • 相关阅读:
    2020 牛客 NOIP 赛前集训营 提高级(第四场) B-色球 平衡树模板
    P4084 [USACO17DEC]Barn Painting G
    CSP-S 2020
    CQOI 2020省选
    我回来了
    hdu3605(二分图多重匹配伪模板)
    舞动的夜晚(二分图的必须边和可行边)
    poj3436(最大流+拆点)
    P2954([USACO09OPEN]移动牛棚Grazing2,dp)
    CSP-S 2020 游记
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835137.html
Copyright © 2011-2022 走看看