Calculate the intersection points of a straight line in HDU 1466

Source: Internet
Author: User

 

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 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.