ZZULIOJ 1145: Problematic odometer (2), Java
Question description
A car has an odometer that can display an integer, the number of kilometers the car has traveled. However, there is a problem with this odometer: it always changes from 3 to 5, skipping the number 4. This is true for all digits on the odometer (ones, tens, hundreds, etc.). For example, if the odometer shows 15339, after the car has traveled 1 kilometer, the odometer will show 15350.
enter
Enter an integer num, which represents the value displayed by the odometer. The length must not exceed 9 digits and must not contain the integer 4.
output
Output an integer representing the actual mileage traveled.
Sample inputCopy
150
Sample outputCopy
117
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(bf.readLine());
int res = 0;
for (int i = 0; n > 0; i++) {
int x = n % 10;
if (x > 4) res += (int) ((x - 1) * Math.pow(9, i));
else res += (int) (x * Math.pow(9, i));
n /= 10;
}
bw.write(res + "\n");
bw.close();
}
}