Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package berlin;
- import java.io.File;
- import java.util.Random;
- import java.util.Scanner;
- public class Berlin {
- public static void main(String[] args) throws Exception {
- String fileName = "D:\\WdTIGS\\berlin52.txt";
- String logRecord;
- File dataFile = new File(fileName);
- String[] splitLine = null;
- Scanner scanner = new Scanner(dataFile);
- int size = Integer.valueOf(scanner.nextLine());
- int[] turysta = new int[5];
- Random rand = new Random();
- for (int i = 0; i < turysta.length; i++){
- turysta[i] = 1 + rand.nextInt(size);
- }
- int[][] tab = new int[size][size];
- int i = 0;
- while (scanner.hasNextLine()) {
- logRecord = scanner.nextLine();
- splitLine = logRecord.split("\\s");
- for (int j = 0; j < splitLine.length; j++) {
- if (!"0".equals(splitLine[j])) {
- tab[i][j] = Integer.valueOf(splitLine[j]);
- tab[j][i] = Integer.valueOf(splitLine[j]);
- }
- }
- i++;
- }
- System.out.print("Turysta: ");
- for(int j = 0; j < turysta.length; j++)
- {
- System.out.printf("%s ", turysta[j]);
- }
- System.out.println("\n---");
- int trasa = countDistance(tab, turysta);
- System.out.println("---");
- System.out.printf("trasa: %s\n\n",trasa);
- }
- private static int countDistance(int[][] tab, int turysta[]) {
- int trasa = 0;
- int b;
- for (int i = 0; i < turysta.length; i++) {
- int a = turysta[i];
- if (i == (turysta.length - 1)){
- b = turysta[0];
- } else {
- b = turysta[i+1];
- }
- trasa += tab[a-1][b-1];
- System.out.printf("Pkt. %s :: a: %2d, b: %2d :: dystans: %s\n", i+1, a, b, tab[a-1][b-1]);
- }
- return trasa;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement