Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. package berlin;
  2. import java.io.File;
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class Berlin {
  7.  
  8. public static void main(String[] args) throws Exception {
  9.  
  10. String fileName = "D:\\WdTIGS\\berlin52.txt";
  11. String logRecord;
  12. File dataFile = new File(fileName);
  13. String[] splitLine = null;
  14. Scanner scanner = new Scanner(dataFile);
  15. int size = Integer.valueOf(scanner.nextLine());
  16.  
  17. int[] turysta = new int[5];
  18. Random rand = new Random();
  19. for (int i = 0; i < turysta.length; i++){
  20. turysta[i] = 1 + rand.nextInt(size);
  21. }
  22.  
  23. int[][] tab = new int[size][size];
  24.  
  25. int i = 0;
  26.  
  27. while (scanner.hasNextLine()) {
  28. logRecord = scanner.nextLine();
  29. splitLine = logRecord.split("\\s");
  30.  
  31. for (int j = 0; j < splitLine.length; j++) {
  32. if (!"0".equals(splitLine[j])) {
  33. tab[i][j] = Integer.valueOf(splitLine[j]);
  34. tab[j][i] = Integer.valueOf(splitLine[j]);
  35. }
  36. }
  37. i++;
  38. }
  39. System.out.print("Turysta: ");
  40. for(int j = 0; j < turysta.length; j++)
  41. {
  42. System.out.printf("%s ", turysta[j]);
  43. }
  44. System.out.println("\n---");
  45. int trasa = countDistance(tab, turysta);
  46. System.out.println("---");
  47. System.out.printf("trasa: %s\n\n",trasa);
  48.  
  49. }
  50. private static int countDistance(int[][] tab, int turysta[]) {
  51. int trasa = 0;
  52. int b;
  53. for (int i = 0; i < turysta.length; i++) {
  54. int a = turysta[i];
  55. if (i == (turysta.length - 1)){
  56. b = turysta[0];
  57. } else {
  58. b = turysta[i+1];
  59. }
  60. trasa += tab[a-1][b-1];
  61. System.out.printf("Pkt. %s :: a: %2d, b: %2d :: dystans: %s\n", i+1, a, b, tab[a-1][b-1]);
  62. }
  63. return trasa;
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement