算法 - 里程表故障(Java & JS & Python & C & C++)

题目描述

You are given a car odometer which displays the miles traveled as an integer. The odometer has a defect, however: it proceeds from the digit 2 to the digit 4 and from the digit 7 to the digit 9, always skipping over the digit 3 and 8. This defect shows up in all positions (the one's, the ten's, the hundred's, etc.). For example, if the odometer displays 15229 and the car travels one mile, odometer reading changes to 15240 (instead of 15230).

给你一个汽车里程表,它以整数的形式显示行驶的英里数。然而,里程表有一个缺陷:它从数字2到数字4,从数字7到数字9,总是跳过数字3和8。这个缺陷出现在所有位置(1位,10位,100位,等等)。例如,如果里程表显示15229,而汽车行驶了一英里,则里程表读数变为15240(而不是15230)。

输入描述

Each line of input contains a positive integer in the range 1..999999999 which represents an odometer reading. (Leading zeros will not appear in the input.) The end of input is indicated by a line containing a single 0. You may assume that no odometer reading will contain the digit 3 and 8.

每一行输入的数范围是[1,999999999 ],表示里程表读数。(前导零不会出现在输入中。)输入的结束由包含一个0的行表示。您可能会认为里程表读数中没有数字3和8。

输出描述

Each line of input will produce exactly one line of output, which will contain: the odometer reading from the input, a colon, one blank space, and the actual number of miles traveled by the car.

每一行输入将产生一行输出,其中将包含:从输入读取的里程表、一个冒号、一个空格和汽车实际行驶的英里数。

用例

输入15
输出12
输入2005
输出1028
输入250
输出160
输入1500
输出768
输入999999
输出262143

题目解析

这题和算法 - 靠谱的车_伏城之外的博客-CSDN博客

一样,只是靠谱的车是九进制,本题是八进制。

JS算法源码

/* JavaScript Node ACM模式 控制台输入获取 */
const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on("line", (line) => {
  const arr = line.split("").map((ele) => parseInt(ele));

  let correct = 0;

  for (let i = 0; i < arr.length; i++) {
    let fault = arr[i];

    if (fault > 8) fault--;
    if (fault > 3) fault--;

    for (let j = arr.length - i - 1; j > 0; j--) {
      fault *= 8;
    }

    correct += fault;
  }

  console.log(correct);
});

Java算法源码

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int[] arr = Arrays.stream(sc.nextLine().split("")).mapToInt(Integer::parseInt).toArray();

        int correct = 0;

        for (int i = 0; i < arr.length; i++) {
            int fault = arr[i];

            if (fault > 8) fault--;
            if (fault > 3) fault--;

            for (int j = arr.length - i - 1; j > 0; j--) {
                fault *= 8;
            }

            correct += fault;
        }

        System.out.println(correct);
    }
}

Python算法源码

# 输入获取
arr = list(map(int, list(input())))


# 算法入口
def solution():
    correct = 0

    for i in range(len(arr)):
        fault = arr[i]

        if fault > 8:
            fault -= 1

        if fault > 3:
            fault -= 1

        for j in range(len(arr) - i - 1, 0, -1):
            fault *= 8

        correct += fault

    return correct


# 算法调用
print(solution())

C算法源码

#include <stdio.h>
#include <string.h>

#define MAX_NUM_LEN 10

int main() {
    char num[MAX_NUM_LEN];
    scanf("%s", num);

    int correct = 0;

    size_t len = strlen(num);

    for (int i = 0; i < len; i++) {
        int fault = num[i] - '0';

        if (fault > 8) fault--;
        if (fault > 3) fault--;

        for (size_t j = len - i - 1; j > 0; j--) {
            fault *= 8;
        }

        correct += fault;
    }

    printf("%d\n", correct);

    return 0;
}

C++算法源码

#include<bits/stdc++.h>
using namespace std;

int main() {
    string num;
    cin >> num;

    int correct = 0;

    for (int i = 0; i < num.length(); i++) {
        int fault = num[i] - '0';

        if (fault > 8) fault--;
        if (fault > 3) fault--;

        for (size_t j = num.length() - i - 1; j > 0; j--) {
            fault *= 8;
        }

        correct += fault;
    }

    cout << correct << endl;

    return 0;
}

 

注意

上面代码我们需要注意,

    if (fault > 8) fault--;
    if (fault > 3) fault--;

顺序不能颠倒,否则当fault=9时,理论上应该变为7,如果上面代码颠倒的话,

    if (fault > 3) fault--;
    if (fault > 8) fault--;
则fault=9时,最终fault只能变为8,会产生错误结果。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伏城之外

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值