Advertisement
thienlang

ToaDo

Jan 6th, 2015
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. struct ToaDo
  6. {
  7.     float x;
  8.     float y;
  9. };
  10.  
  11. float tinhKhoangCach ( ToaDo A, ToaDo B)
  12. {
  13.     float result = sqrt( (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
  14.     return result;
  15. }
  16.  
  17. void NhapToaDo( ToaDo & A)
  18. {
  19.     printf("Hoanh do: ");
  20.     scanf("%f", &A.x);
  21.  
  22.     printf("Tung do: ");
  23.     scanf("%f", &A.y);
  24. }
  25.  
  26. void inputArrayPoint( ToaDo* A, int n)//n la so phan tu cua mang
  27. {
  28.     for ( unsigned int i = 0; i < n; i++)
  29.     {
  30.         printf("Nhap diem thu %d: \n", i);
  31.         NhapToaDo(A[i]);
  32.     }
  33. }
  34.  
  35. ToaDo distanceToO( ToaDo *A, int n)//n la so phan tu cua mang ToaDo A
  36. {
  37.     ToaDo O;
  38.     O.x = 0;
  39.     O.y = 0;
  40.     float Min = tinhKhoangCach(A[0], O);
  41.     ToaDo TDMin;
  42.     TDMin = A[0];
  43.     for ( unsigned int i = 1; i < n; i++)
  44.     {
  45.         float temp = tinhKhoangCach(A[i], O);
  46.         if ( Min > temp)
  47.         {
  48.             Min = temp;
  49.             TDMin = A[i];
  50.         }
  51.     }
  52.  
  53.     return TDMin;
  54. }
  55.  
  56. void main()
  57. {
  58.     ToaDo A[100];
  59.     int n;
  60.     printf("Nhap so phan tu mang toa do: ");
  61.     scanf( "%d", &n);
  62.     inputArrayPoint(A, n);
  63.  
  64.     ToaDo X = distanceToO(A,n);
  65.  
  66.     printf("\nDiem gan O nhat la: (%.2f , %.2f)", X.x, X.y);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement