Untitled
unknown
plain_text
4 years ago
2.8 kB
9
Indexable
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Main {
static boolean checkPrime(String s){
int x=Integer.parseInt(s);
boolean flag=true;
for (int i=2; i<=x/2 ;i++){
if(x%i==0){
flag=false;
break;
}
}
if(x==1){
flag=false;
}
return flag;
}
static boolean checkPattern(int kthIndex,int prev,int count){
if(count==0){
return true;
}
return kthIndex == (prev - 1) || kthIndex == (prev + 1);
}
public static void main(String[] args) {
int sum = 0;
int prevStep=0;
int maxNumber;
int indexOfMax=0;
int i;
int count=0;
try {
FileWriter fw=new FileWriter("myFile.txt");
fw.write("215\n" +
"193 124\n" +
"117 237 442\n" +
"218 935 347 235\n" +
"320 804 522 417 345\n" +
"229 601 723 835 133 124\n" +
"248 202 277 433 207 263 257\n" +
"359 464 504 528 516 716 871 182\n" +
"461 441 426 656 863 560 380 171 923\n" +
"381 348 573 533 447 632 387 176 975 449\n" +
"223 711 445 645 245 543 931 532 937 541 444\n" +
"330 131 333 928 377 733 017 778 839 168 197 197\n" +
"131 171 522 137 217 224 291 413 528 520 227 229 928\n" +
"223 626 034 683 839 053 627 310 713 999 629 817 410 121\n" +
"924 622 911 233 325 139 721 218 253 223 107 233 230 124 233");
fw.close();
FileReader fr = new FileReader("myFile.txt");
Scanner reader = new Scanner(fr);
while (reader.hasNextLine()) {
String input = reader.nextLine();
String[] splittedInput = input.split(" ");
maxNumber=0;
for (i = 0; i < splittedInput.length; i++) {
if(Integer.parseInt(splittedInput[i])>maxNumber &&(!checkPrime(splittedInput[i]))&&checkPattern(i,prevStep,count)){
maxNumber=Integer.parseInt(splittedInput[i]);
indexOfMax= i;
}
}
prevStep=indexOfMax;
sum+=maxNumber;
System.out.println("The largest non-prime number of the row "+i +" is "+ maxNumber);
count++;
}
} catch (IOException e){
e.printStackTrace();
}
System.out.println("The maximum sum is "+sum);
}
}Editor is loading...