Created
July 15, 2011 03:23
-
-
Save anupsavvy/1083995 to your computer and use it in GitHub Desktop.
Triangle Problem - By starting at the top of the triangle and moving to adjacent numbers on the row below, find the maximum total from top to bottom.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.triangle; | |
import java.io.File; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.StringTokenizer; | |
public class Triangle { | |
public static void main(String args[]){ | |
try{ | |
File file = new File("/users/sawant/downloads/triangle.txt"); | |
BufferedReader br = new BufferedReader(new FileReader(file)); | |
StringTokenizer tokenizer = null; | |
int count = 0; | |
int position = 0; | |
int runningSum = 0; | |
int num1 = 0,num2 = 0; | |
String line = br.readLine(); | |
while(line!=null){ | |
tokenizer = new StringTokenizer(line," "); | |
position = count; | |
while(tokenizer.hasMoreTokens()){ | |
if(count == 0) | |
num1 = Integer.parseInt(tokenizer.nextToken()); | |
if(count == -1){ | |
num2 = Integer.parseInt(tokenizer.nextToken()); | |
break; | |
} | |
count--; | |
} | |
if(num1 > num2){ | |
runningSum += num1; | |
} | |
else{ | |
runningSum += num2; | |
position++; | |
} | |
count = position; | |
num1=num2=0; | |
line = br.readLine(); | |
} | |
System.out.println("Highest total : " + runningSum); | |
}catch(IOException e){ | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment