Files
prog-intro-2025/java/sum/SumBigIntegerOctal.java
me 2f05f238e9
All checks were successful
Markup Tests / test (push) Successful in 8s
Markdown to Html Tests / test (push) Successful in 17s
update
2026-02-17 09:32:08 +03:00

46 lines
1.2 KiB
Java

package sum;
import java.math.BigInteger;
/**
* @author Nikita Doschennikov (me@fymio.us)
*/
public class SumBigIntegerOctal {
public static void main(String[] args) {
BigInteger res = new BigInteger("0");
for (String arg : args) {
StringBuilder builder = new StringBuilder();
for (char c : arg.toCharArray()) {
if (!Character.isWhitespace(c)) {
builder.append(c);
} else {
res = res.add(compute(builder.toString()));
builder = new StringBuilder();
}
}
res = res.add(compute(builder.toString()));
}
System.out.println(res);
}
static BigInteger compute(String num) {
BigInteger res = new BigInteger("0");
int numLength = num.length();
if (num.isEmpty()) {
res = res.add(BigInteger.ZERO);
} else if (
num.charAt(numLength - 1) == 'o' || num.charAt(numLength - 1) == 'O'
) {
res = res.add(
new BigInteger(num.substring(0, num.length() - 1), 8)
);
} else {
res = res.add(new BigInteger(num));
}
return res;
}
}