그래서 나는 추가 된 값으로 피라미드를 만들려고합니다. 전의. 1 번 행에는 (5)가 있고 2 번 행에는 (6,7) 행 3에는 (12,10,7)이 있습니다. 목표는 첫 번째 행의 가장 높은 값을 추가하는 것이며, 다음 행의 가장 높은 연결 하위 값을 추가하는 것입니다. 따라서이 경우에는 5 + 7 + 10을 추가하여 22의 결과를 얻습니다. 3 행에서 12를 사용할 수없는 이유는 위의 숫자의 하위 항목을 사용해야하기 때문입니다 (각 상위 항목에는 2 명의 어린이).Java Scanner/Array Pyramid
내 접근 방식은 배열에 int 값을 줄 단위로로드하는 데 스캐너를 사용하고 이전 줄의 가장 높은 하위 값의 위치를 어떻게 든 색인화하여 누적 합계에 추가합니다. 여기에 ... 내가 지금까지 가지고있는 코드 ...
// 데이터 파일의
6 7
(12) (10) (7)
5
// 그게 다야. 현재public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(new File("https://stackoverflow.com/users/joe/desktop/data.txt"));
} catch (FileNotFoundException e) {
System.out.println("File not found.");
e.printStackTrace();
} //reads the file
int[] a = new int[100]; //establishes new array with a max size of 100
int i = 0; //placeholder for array position
int result = 0;
int total = 0;
while(scanner.hasNextLine()){ //loops through line
a[i++] = scanner.nextInt(); //adds int to array
if(i == 0){ //does this belong here?
result = a[0];
}
else{
if(a[i-1] >= a[i+1]){
result = a[i-1];
}
else if(a[i-1] <= a[i+1]){
result = a[i+1];
}
}
}
total = total + result;
scanner.close();
System.out.print(Arrays.toString(a));
System.out.println("\n" + total);
}
}
,이 출력됩니다 [5,6,7,12,10700000, 100 개 위치로 ... 최대]
5
스캐너가 한 줄을 읽고 배열로로드하고 반복하고 다음 줄의 배열에서 가장 높은 하위 값을 저장하려면 어떻게해야합니까?
오, 나는 그것이 엉망이라고 알고 있었고, if/else는 작동하지 않지만 수정을 할 것입니다. 고마워요! –