/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javaapplication7; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Scanner; import java.util.Arrays; import static java.util.Arrays.stream; import java.util.LinkedList; /** * * @author hilal */ public class JavaApplication7 { public static void main(String[] args) throws IOException { LinkedList ar = new LinkedList<>(); try { File myObj = new File("deneme.txt"); Scanner myReader = new Scanner(myObj); while (myReader.hasNextLine()) { String data = myReader.nextLine(); ar.add(data); } myReader.close(); } catch (FileNotFoundException e) { System.out.println("An error occurred."); e.printStackTrace(); } int[][] array = new int[ar.size()][ar.size()]; for(int i = 0; i < ar.size(); i++){ for(int j = 0; j < ar.size(); j++){ if(j > i){ array[i][j] = 0; }else{ if(ar.get(i).split(" ")[j] != null){ array[i][j] = Integer.parseInt(ar.get(i).split(" ")[j]); } } } } int sum = array[0][0]; int max = 0; int index = 0; int index_y = 0; int old_index = 0; int old_index_y = 0; for (int i = 1; i < array[0].length; i++) { old_index = index; old_index_y = index_y; max = 0; for (int j = 0; j < array[0].length; j++) { if (array[i][j] > max && isPrime(array[i][j]) == false) { max = array[i][j]; index = i; index_y = j; } } if(old_index +1 == index && (index_y == old_index_y-1 || index_y == old_index_y+1)){ sum+=max; }else{ if(array[old_index+1][old_index_y-1]>array[old_index+1][old_index_y+1] && isPrime(array[old_index+1][old_index_y-1])==false){ sum += array[old_index+1][old_index_y-1]; index = old_index+1; index_y = old_index_y-1; }else{ if(isPrime(array[old_index+1][old_index_y+1])==false){ sum += array[old_index+1][old_index_y+1]; index = old_index + 1; index_y = old_index_y+1; } } } } System.out.println(sum); } public static boolean isPrime(int N) { boolean ispr = true; if (N <= 1) { ispr = false; } for (int i = 2; i <= N-1; i++) { if (N % i == 0) { ispr = false; break; } } return ispr; } }