DescriptionThere are n straight lines on the plane, and there are no three-wire common points, asking how many different cross-points these straight lines can have.
For example, if n=2, the number of possible intersections is 0 (parallel) or 1 (not parallel).InputThe input data consists of multiple test instances, one row per test instance, each containing a positive integer n (n<=20), and n representing the number of lines.OutputEach test instance corresponds to a row of output, from small to large listing all intersecting scenarios, where each number is the number of possible intersections, separated by a space between the integers in each line.Sample Input
23
Sample Output
0 10 2 3
1 lines: 0
2 lines: 0 1
3 lines: 0 2 3
4 Lines: 0 3 4 5 6
...
The line can be divided into two groups, the first group is parallel to each other straight lines, the second group is a free line, free straight line is not parallel with parallel lines;
Set the line is always n, set the first group has I line, then the second group N-i a straight line.
analyzed by 4 lines:
1, when i=4,n-i=0, the number of intersections: 0
2, when I=3,n-i=1, the number of intersections: 3 Parallel lines and 1 free line intersection number + 1 free line can form the number of intersections, that is 3+0=3
3, when i=2,n-i=2, the number of intersections: 2*2+{0,1}={4,5}
4, when i=1,n-i=3, the number of intersections: 1*3+{0,2,3}={3,5,6}
each free line has an intersection with each parallel line, and the number of intersections of the J free and I parallel lines is j*i, so the intersection of the n lines equals the intersections of the free and parallel lines plus the intersections formed within the free line
1#include <cstdio>2#include <string.h>3 intMain ()4 {5 intN,i,,k, Free;6 intd[ -][ $];//D[i][j] Indicates that the I line has a J intersection Point7memset (D,0,sizeof(d));8 for(i =1; I < +; i++)9 {Tenc[n][0]=1;//An intersection of 0 always exists One for( Free=0; Free<= i; Free++)//free represents the number of straight lines A { - for(k =0; K < Free*( Free+1)/2; k++) - { the if(d[ Free][k] = =1) - { -d[i][ Free* (I- Free) +k]=1;//free* (i-free) +k indicates the number of intersections of the free straight line of the I line (k is the number of intersections of the free line) - } + } - } + } A while(SCANF ("%d", &n)! =EOF) at { - for(i =0; I <= N (n1)/2; i++) - { - if(i = =0) -printf"0"); - Else in { - if(D[n][i]) to { +printf"%d", i); - } the } * } $printf"\ n");Panax Notoginseng } - return 0; the}
Hangzhou Electric 1466 Calculate the line of intersection