solved 3a
This commit is contained in:
41
src/Problem3.java
Normal file
41
src/Problem3.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package src;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Problem3 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String filePath = "inputs/inputProblem3.txt";
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
|
||||
String line;
|
||||
long total = 0;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
total += maxTwoDigit(line);
|
||||
}
|
||||
System.out.println(total);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static int maxTwoDigit(String digits) {
|
||||
int maxDigit = -1;
|
||||
int maxValue = 0;
|
||||
for (char c : digits.toCharArray()) {
|
||||
int digit = c - '0';
|
||||
if (maxDigit >= 0) {
|
||||
int value = maxDigit * 10 + digit;
|
||||
if (value > maxValue) {
|
||||
maxValue = value;
|
||||
}
|
||||
}
|
||||
if (digit > maxDigit) {
|
||||
maxDigit = digit;
|
||||
}
|
||||
}
|
||||
|
||||
return maxValue;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user