Untitled
raw download clone
C
views 27
,
size 2233 b
#include <stdio.h>

int main(void)
{
    long int T, N;
    long int x[105], y[105]; // (x[i],y[i])
    long int col[105][105];
    long int temp[105];
    long int count = 0, temp_count = 0;
    long int dx, dy;

    scanf("%ld", &T);
    while (T--)
    {
        count = 0;
        for (int i = 0;i < 105;i++)
        {
            for (int j = 0;j < 105;j++)
            {
                col[i][j] = 0;
            }    
        }
            
        scanf("%ld", &N);
        for (int i = 1;i <= N;i++)
        {
            scanf("%ld %ld", &x[i], &y[i]);
        }    
    
        for (int i = 1;i <= N;i++)
        {
            for (int j = i + 1;j <= N;j++)
            {
                if (col[i][j] != 0) continue; //i,j共線過則點2跳下一個
                    
                dx = x[j] - x[i];
                dy = y[j] - y[i];
                temp_count = 0;

                for (int k = j + 1 ; k <= N ; k++)
                {
                    if ( col[i][j] == 0 && (dx * (y[k] - y[i]) == dy * (x[k] - x[i])) ) //i,j未共線過 , 點k符合i,j線的公式
                    {
                        count++;//編號+1
                        col[i][j] = count;
                        col[i][k] = count;
                        col[j][k] = count;
                        temp[temp_count] = k;//點k放入暫存
                        temp_count++;//暫存數+1
                    }
                    else if (dx * (y[k] - y[i]) == dy * (x[k] - x[i])) //i,j共線過了
                    { 
                        col[i][k] = count;
                        col[j][k] = count;
                        temp[temp_count] = k;
                        temp_count++;
                    }
                }
                if (temp_count > 1) //i,j以外的點紀錄共線
                {
                    for (int k = 0;k < temp_count - 1;k++)
                    {
                        for (int l = k + 1;l < temp_count; l++)
                            col[ temp[k] ][ temp[l] ] = count;
                    }
                        
                }
            }
        }
        printf("%d\n", count);
    }
}
close fullscreen
Login or Register to edit or fork this paste. It's free.