Problem Description
Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is very easy, after all we are now attending an exam, not a contest :)
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.
Note:
You can assume that two segments would not intersect at more than one point.
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.
Note:
You can assume that two segments would not intersect at more than one point.
Input
Input contains multiple test cases. Each test case contains a integer N (1=N<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which are coordinates of the segment’s ending.
A test case starting with 0 terminates the input and this test case is not to be processed.
A test case starting with 0 terminates the input and this test case is not to be processed.
Output
For each case, print the number of intersections, and one line one case.
Sample Input
2 0.00 0.00 1.00 1.00 0.00 1.00 1.00 0.00 3 0.00 0.00 1.00 1.00 0.00 1.00 1.00 0.000 0.00 0.00 1.00 0.00 0
Sample Output
1 3题意:给出n条线段,求交点数,注意不存在重合的情况思路:用叉乘来判断代码如下:import java.io.BufferedReader; import java.io.FileReader; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Scanner; public class Main implements Runnable { private static final boolean DEBUG = false; private Scanner cin; private PrintWriter cout; private int n; private static final int N = 101; private static final Line[] lines = new Line[N]; class Point { double x, y; } class Line { Point a, b; } private void init() { try { if (DEBUG) { cin = new Scanner(new BufferedReader(new FileReader("f:\\OJ\\uva_in.txt"))); } else { cin = new Scanner(new BufferedReader(new InputStreamReader(System.in))); } cout = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); } catch (Exception e) { e.printStackTrace(); } } private boolean input() { n = cin.nextInt(); if (n == 0) return false; for (int i = 0; i < n; i++) { lines[i] = new Line(); Point a = new Point(); a.x = cin.nextDouble(); a.y = cin.nextDouble(); lines[i].a = a; Point b = new Point(); b.x = cin.nextDouble(); b.y = cin.nextDouble(); lines[i].b = b; } return true; } private double cross(Point a, Point b) { double x1 = a.x, y1 = a.y, x2 = b.x, y2 = b.y; return x1 * y2 - y1 * x2; } private boolean isInterSection(Line l1, Line l2) { Point p1 = new Point(); p1.x = l1.b.x - l1.a.x; p1.y = l1.b.y - l1.a.y; Point p2 = new Point(); p2.x = l2.b.x - l2.a.x; p2.y = l2.b.y - l2.a.y; double c2 = cross(p1, p2); p1.x = l2.a.x - l1.a.x; p1.y = l2.a.y - l1.a.y; double c1 = cross(p1, p2); p1.x = l2.a.x - l1.a.x; p1.y = l2.a.y - l1.a.y; p2.x = l1.b.x - l1.a.x; p2.y = l1.b.y - l1.a.y; double c3 = cross(p1, p2); if (c2 < 0) { c2 = -c2; c1 = -c1; c3 = -c3; } if (c2 != 0 && c1 >= 0 && c1 <= c2 && c3 >= 0 && c3 <= c2) return true; else return false; } private void solve() { int ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (isInterSection(lines[i], lines[j])) ans++; } } cout.println(ans); cout.flush(); } @Override public void run() { init(); while (input()) { solve(); } } public static void main(String[] args) { // write your code here new Thread(new Main()).start(); } }