fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5.  
  6. struct point
  7. {
  8. double x;
  9. double y;
  10. } *points;
  11.  
  12. int main (int argc, char *argv[])
  13. {
  14. int n = atoi(argv[1]);
  15. points = calloc(n, sizeof(struct point));
  16. srand((unsigned) time(NULL));
  17.  
  18. for (int i=0; i<n; ++i){
  19. points[i].x = rand() / (double) RAND_MAX;
  20. points[i].y = rand() / (double) RAND_MAX;
  21. }
  22.  
  23. double maxlen = INFINITY;
  24. int p1,p2;
  25.  
  26. for (int i=0; i<n-1; ++i){
  27. for (int j=i+1; j<n; ++j){
  28. double dx = points[j].x - points[i].x;
  29. double dy = points[j].y - points[i].y;
  30. double len = sqrt(dx*dx + dy*dy);
  31. if (len < maxlen){
  32. maxlen = len;
  33. p1 = i;
  34. p2 = j;
  35. }
  36. }
  37. }
  38.  
  39. for (int i=0; i<n; ++i){
  40. printf("%d (%f, %f) %c \n", i, points[i].x, points[i].y, ((i==p1) || (i==p2)) ? '*' : ' ');
  41. }
  42.  
  43. free(points);
  44. return 0;
  45. }
Runtime error #stdin #stdout 0s 2156KB
stdin
Standard input is empty
stdout
Standard output is empty