fork(2) download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. int t;
  6. t = sc.nextInt();
  7. for (; 0 < t; t--) {
  8. int iloscPunktow = sc.nextInt();
  9. String[] nazwaTab = new String[iloscPunktow];
  10. int[] xTab = new int[iloscPunktow];
  11. int[] yTab = new int[iloscPunktow];
  12. for (int i = 0; i < iloscPunktow; i++) {
  13. String punkt = sc.next();
  14. int x = sc.nextInt();
  15. int y = sc.nextInt();
  16. nazwaTab[i] = punkt;
  17. xTab[i] = x;
  18. yTab[i] = y;
  19. }
  20. sortJeden(nazwaTab, xTab, yTab);
  21. }
  22. }
  23.  
  24. private static void sortJeden(String[] punkt, int[] punktX, int[] punktY) {
  25. double[] odleploscPunktu = new double[punkt.length];
  26. for (int j = 0; j < punkt.length; j++) {
  27. if (punktX[j] == 0) {
  28. odleploscPunktu[j] = punktY[j];
  29. } else if (punktY[j] == 0) {
  30. odleploscPunktu[j] = punktX[j];
  31. } else {
  32. odleploscPunktu[j] = Math.sqrt(Math.pow(punktX[j], 2) + Math.pow(punktY[j], 2));
  33. }
  34. }
  35.  
  36. int tempX;
  37. int tempY;
  38. String tempPunkt;
  39. double tempOdleglosc;
  40. int zmiana = 1;
  41. while (zmiana > 0) {
  42. zmiana = 0;
  43. for (int k = 0; k < punkt.length - 1; k++) {
  44. if (odleploscPunktu[k] > odleploscPunktu[k + 1]) {
  45. tempOdleglosc = odleploscPunktu[k + 1];
  46. odleploscPunktu[k + 1] = odleploscPunktu[k];
  47. odleploscPunktu[k] = tempOdleglosc;
  48. tempPunkt = punkt[k + 1];
  49. punkt[k + 1] = punkt[k];
  50. punkt[k] = tempPunkt;
  51. tempX = punktX[k + 1];
  52. punktX[k + 1] = punktX[k];
  53. punktX[k] = tempX;
  54. tempY = punktY[k + 1];
  55. punktY[k + 1] = punktY[k];
  56. punktY[k] = tempY;
  57. zmiana++;
  58. }
  59. }
  60. }
  61.  
  62. for (int l = 0; l < punkt.length; l++) {
  63. System.out.println(punkt[l] + " " + punktX[l] + " " + punktY[l] + " odleglosc = " + odleploscPunktu[l]);
  64. }
  65. System.out.println();
  66. }
  67.  
  68. private static Scanner sc = new Scanner(System.in);
  69. }
Success #stdin #stdout 0.08s 2184192KB
stdin
3
9
A 1 1
B 3 3
C 2 2
D 5 5
E 4 4
F 7 7
G 6 6
H 9 9
I 8 8

3
X 5 -5
Y 2 2
Z 1 1

4
Ax 10 10
Bx 10 9
Cx 8 10
Dx 11 7
stdout
A 1 1 odleglosc = 1.4142135623730951
C 2 2 odleglosc = 2.8284271247461903
B 3 3 odleglosc = 4.242640687119285
E 4 4 odleglosc = 5.656854249492381
D 5 5 odleglosc = 7.0710678118654755
G 6 6 odleglosc = 8.48528137423857
F 7 7 odleglosc = 9.899494936611665
I 8 8 odleglosc = 11.313708498984761
H 9 9 odleglosc = 12.727922061357855

Z 1 1 odleglosc = 1.4142135623730951
Y 2 2 odleglosc = 2.8284271247461903
X 5 -5 odleglosc = 7.0710678118654755

Cx 8 10 odleglosc = 12.806248474865697
Dx 11 7 odleglosc = 13.038404810405298
Bx 10 9 odleglosc = 13.45362404707371
Ax 10 10 odleglosc = 14.142135623730951