fork(1) download
  1. #include<iostream>
  2. #include<string>
  3. #include<math.h>
  4. using namespace std;
  5. class MyClass
  6. {
  7. string name;
  8. int x;
  9. int y;
  10. public:
  11. int myvariable;
  12. void set(string _name, int _x, int _y)
  13. {
  14. name = _name;
  15. x = _x;
  16. y = _y;
  17. myvariable = sqrt(x*x + y*y);
  18.  
  19. }
  20. MyClass()
  21. {
  22.  
  23. name = " ";
  24. x = 0;
  25. y = 0;
  26. myvariable = 0;
  27.  
  28. }
  29. void print()
  30. {
  31. cout << name << " " << x << " " << y << endl;
  32. }
  33.  
  34. };
  35. void quicksort(MyClass *tablic, int lewy, int prawy)
  36. {
  37. int pivot = tablic[(lewy + prawy) / 2].myvariable;
  38. int i, j;
  39. MyClass bufor;
  40. i = lewy;
  41. j = prawy;
  42. do
  43. {
  44. while (tablic[i].myvariable<pivot) i++;
  45. while (tablic[j].myvariable>pivot) j--;
  46. if (i <= j)
  47. {
  48. bufor = tablic[i];
  49. tablic[i] = tablic[j];
  50. tablic[j] = bufor;
  51. i++;
  52. j--;
  53. }
  54. } while (i <= j);
  55. if (j>lewy) quicksort(tablic, lewy, j);
  56. if (i<prawy) quicksort(tablic, i, prawy);
  57. }
  58.  
  59. int main()
  60. {
  61. MyClass *tablica;
  62. string name;
  63. int x;
  64. int y;
  65. int n;
  66. int test;
  67. cin >> test;
  68. for (int g = 0; g < test; g++)
  69. {
  70. cin >> n;
  71. tablica = new MyClass[n];
  72. for (int i = 0; i < n; i++)
  73. {
  74. cin >> name;
  75. cin >> x;
  76. cin >> y;
  77. tablica[i].set(name, x, y);
  78.  
  79. }
  80.  
  81.  
  82. quicksort(tablica, 0, n - 1);
  83.  
  84. for (int i = 0; i < n; i++)
  85. {
  86. tablica[i].print();
  87. }
  88. }
  89. delete [] tablica;
  90. return 0;
  91. }
Success #stdin #stdout 0s 3476KB
stdin
1
2
A 10 11
B 0 15
stdout
A 10 11
B 0 15