Problem: calculate all the intersection points composed of n straight lines.
Analysis: the first three straight lines are well analyzed and easy to understand. Therefore, we can add one to the fourth line from the third to analyze all the subsequent situations.
When n = 4:
1. all four straight lines are parallel, with no intersection. among the three parallel nodes, the intersection points: (n-1) * 1 + 0 = 3; 3. two of them are parallel, and the intersection of the other two straight lines can be parallel or intersection, so the intersection data is: (n-2) * 2 + 0 = 4, (n-2) * 2 + 1 = 54. four straight lines are parallel to each other, points of intersection (n-3) * 3 + 3 line intersection: (n-3) * 3 + 0 = 3, (n-3) * 3 + 2 = 5, (n-3) * 3 + 3 = 6
Discovery: number of intersection points of m straight lines = number of intersection points of r straight lines and (m-r) parallel lines + intersection of r straight lines = (m-r) * intersection points between r + r straight lines. AC code:
# Include <iostream> # include <cstdio> # include <cstring> using namespace std; int dp [21] [202]; int main () {int n, I, j; memset (dp, 0, sizeof (dp); for (I = 0; I <21; I ++) {dp [I] [0] = 1; // 1 indicates that the root line of I has j (0) points. This possibility} for (n = 2; n <21; n ++) {for (I = 1; I <n; I ++) {for (j = 0; j <201; j ++) {if (dp [n-I] [j] = 1) // If the n-I root line contains j points {dp [n] [j + I * (n-I)] = 1 ;}}}} while (scanf ("% d", & n )! = EOF) {printf ("0"); // all have 0 intersections for (I = 1; I <201; I ++) {if (dp [n] [I] = 1) {printf ("% d", I) ;}} printf ("\ n") ;}return 0 ;}