Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define MAX_POINTS 1000
- typedef struct Coordonnees Coordonnees;
- struct Coordonnees
- {
- int x;
- int y;
- };
- // Compare deux coordonées, d'abord selon l'abscisse puis si égalité selon l'ordonnée
- int plusPetit(const void* p1, const void* p2)
- {
- const Coordonnees *point1 = (const Coordonnees*)p1;
- const Coordonnees *point2 = (const Coordonnees*)p2;
- int diff = point1->x - point2->x;
- if (diff != 0) {return (diff);}
- else {return (point1->y - point2->y);}
- }
- int main()
- {
- Coordonnees point[MAX_POINTS];
- Coordonnees milieux[MAX_POINTS*MAX_POINTS];
- int nbPoints = 0;
- int nbMilieux = 0;
- int nbCorrespondance = 0;
- int x0 = 0;
- int y0 = 0;
- int i = 0;
- int j = 0;
- // Lecture des coordonnées
- scanf("%d", &nbPoints);
- for (i = 0; i < nbPoints; i++)
- {
- scanf("%d %d", &point[i].x, &point[i].y);
- }
- // Calcul des milieux
- for (i = 0; i < nbPoints; i++)
- {
- for (j = 0; j < nbPoints; j++)
- {
- x0 = (point[i].x + point[j].x)%2; //Le milieu n'est ajouté
- y0 = (point[i].y + point[j].y)%2; //que si ses coordonnées
- if ( (i != j) && (x0 == 0) && (y0 == 0) ) //sont entieres, et que
- { //les 2 points sont distincs
- milieux[nbMilieux].x = (((point[i].x + point[j].x))/2);
- milieux[nbMilieux].y = (((point[i].y + point[j].y))/2);
- nbMilieux++;
- }
- }
- }
- // Tri des milieux en ordre croissant, pour effectuer des recherches ensuite
- qsort(milieux, nbMilieux,sizeof(milieux[0]), plusPetit);
- // Pour chaque point, je cherche si il appartient au tableau milieux
- for (i = 0; i < nbPoints; i++)
- {
- if (bsearch(&point[i], milieux, nbMilieux, sizeof(milieux[0]), plusPetit))
- {nbCorrespondance++;}
- }
- // Affichage de la sortie
- printf("%d", nbCorrespondance);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment