Cod sursa(job #769544)

Utilizator gicu_01porcescu gicu gicu_01 Data 19 iulie 2012 21:27:21
Problema Trapez Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <stdio.h>
#include <stdlib.h>
struct punct
{
    int x;
    int y;
} a[100];

void sw(int *a,int *b)
{
    int t=*a; *a=*b; *b=t;
}

void qs(int left,int right)
{
    int i,j,p;
    i=left; j=right; p=(i+j)/2;
    while (i<j)
    {
        while ( a[i].x*a[p].y<a[i].y*a[p].x ) i++;
        while ( a[j].y*a[p].x>a[j].x*a[p].y ) j--;
        if (i<=j)
        {
            sw(&a[i].x,&a[j].x);
            sw(&a[i].y,&a[j].y);
            i++; j--;
        }
    }
    if (i<right) qs(i,right);
    if (j>left) qs(left,j);
}

int main()
{
    int i;
    for (i=1; i<=10; i++)
    {
        a[i].x=rand()%100;
        a[i].y=rand()%100;
        printf("%i %i\n",a[i].x,a[i].y);
    }
    qs(1,10);
    for (i=1; i<=10; i++)
    {
        printf("*%i %i\n",a[i].x,a[i].y);
    }
    return 0;
}