poj3668

简单题

View Code
#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <algorithm>

using namespace std;



const int maxn =205;



struct Point

{

    int x, y;

}slope[maxn * maxn], point[maxn];



int n, tot;



bool operator == (const Point &a, const Point &b)

{

    return a.x == b.x && a.y == b.y;

}



bool operator < (const Point &a, const Point &b)

{

    if (a.x == b.x)

        return a.y < b.y;

    return a.x < b.x;

}



void input()

{

    scanf("%d", &n);

    for (int i = 0; i < n; i++)

        scanf("%d %d", &point[i].x, &point[i].y);

}



int gcd(int a, int b)

{

    b = b % a;

    while (b)

    {

        a = a % b;

        swap(a, b);

    }

    return a;

}



void simplify(int &x, int &y)

{

    if (x == 0)

    {

        y = 1;

        return;

    }

    if (y == 0)

    {

        x = 1;

        return;

    }

    if (y < 0)

    {

        x = -x;

        y = -y;

    }

    int g = gcd(x < 0 ? -x : x, y);

    x /= g;

    y /= g;

}



void work()

{

    tot = 0;

    for (int i = 0; i < n - 1; i++)

        for (int j = i + 1; j < n; j++)

        {

            slope[tot].x = point[j].x - point[i].x;

            slope[tot].y = point[j].y - point[i].y;

            simplify(slope[tot].x, slope[tot].y);

//            printf("%d %d\n", slope[tot].x, slope[tot].y);

            tot++;

        }

}



int main()

{

//    freopen("t.txt", "r", stdin);

    input();

    work();

    sort(slope, slope + tot);

    tot = unique(slope, slope + tot) - slope;

    printf("%d\n", tot);

    return 0;

}

你可能感兴趣的:(poj)