华为OD机考E卷200分题 - 跳房子I

最新华为OD机试

题目描述

跳房子,也叫跳飞机,是一种世界性的儿童游戏。

游戏参与者需要分多个回合按顺序跳到第1格直到房子的最后一格。

跳房子的过程中,可以向前跳,也可以向后跳。

假设房子的总格数是count,小红每回合可能连续跳的步教都放在数组steps中,请问数组中是否有一种步数的组合,可以让小红两个回合跳到量后一格?

如果有,请输出索引和最小的步数组合。

注意:

  • 数组中的步数可以重复,但数组中的元素不能重复使用。
  • 提供的数据保证存在满足题目要求的组合,且索引和最小的步数组合是唯一的。

输入描述

第一行输入为每回合可能连续跳的步数,它是int整数数组类型。

第二行输入为房子总格数count,它是int整数类型

备注

  • count ≤ 1000
  • 0 ≤ steps.length ≤ 5000
  • -100000000 ≤ steps ≤ 100000000

输出描述

返回索引和最小的满足要求的步数组合(顺序保持steps中原有顺序)

示例1

输入

[1,4,5,2,2]
7
12

输出

[5, 2]
1

说明

示例2

输入

[-1,2,4,9,6]
8
12

输出

[-1, 9]
1

说明

此样例有多种组合满足两回合跳到最后,譬如:[-1,9],[2,6],其中[-1,9]的索引和为0+3=3,[2,6]的索和为1+4=5,所以索引和最小的步数组合[-1,9]

解题思路

题意

这道题目要求从一个给定的步数数组中找到一个步数组合,使得小红能够通过两次跳跃从第1格跳到第count格,并且这个组合在原数组中的索引和是最小的。输出是该步数组合中的两个步数,顺序保持与steps数组中的顺序一致。

再说的明白一点,在steps数组中选两个数,使其之和等于count,并且这两个数在原数组中的索引和是最小的

与下面这题基本一致:

https://leetcode.cn/problems/two-sum/description/

示例解释

  • 示例1

    • 输入 [1, 4, 5, 2, 2]7

    • 一共有7个格子。步数组合中 [5, 2](5 + 2 =7) 是满足条件的组合。

  • 示例2

    • 输入 [-1, 2, 4, 9, 6]8
    • 一共有8个格子。步数组合中 [-1, 9][2, 6] 都满足条件。
    • 但是,[-1, 9]的索引和为0 + 3 = 3,而 [2, 6]的索引和为1 + 4 = 5,所以选择 [-1, 9]

Java

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // 创建Scanner对象用于读取输入
        Scanner sc = new Scanner(System.in);

        // 读取一行输入,将其转换为int数组steps
        String tmp = sc.nextLine();
        int[] steps =
                Arrays.stream(tmp.substring(1, tmp.length() - 1).split(","))
                        .mapToInt(Integer::parseInt)
                        .toArray();

        // 读取房子总格数count
        int count = Integer.parseInt(sc.nextLine());

        // 初始化最小索引和为最大整数值
        int minIdxSum = Integer.MAX_VALUE;
        // 初始化答案为空字符串
        String ans = "";

        // 使用两层循环遍历数组中的所有可能的组合
        for (int idx1 = 0; idx1 < steps.length; idx1++) {
            for (int idx2 = idx1 + 1; idx2 < steps.length; idx2++) {
                // 获取两个步数
                int step1 = steps[idx1];
                int step2 = steps[idx2];

                // 如果两个步数之和等于count
                if (step1 + step2 == count) {
                    // 计算当前组合的索引和
                    int idxSum = idx1 + idx2;
                    // 如果当前组合的索引和小于已找到的最小索引和
                    if (idxSum < minIdxSum) {
                        // 更新最小索引和
                        minIdxSum = idxSum;
                        // 更新答案
                        ans = "[" + step1 + ", " + step2 + "]";
                    }
                    // 找到满足条件的组合后,跳出内层循环
                    break;
                }
            }
        }

        // 输出结果
        System.out.println(ans);
    }
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051

Python

def main():
    # 读取一行输入,将其转换为 int 列表 steps
    steps = list(map(int, input()[1:-1].split(',')))

    # 读取房子总格数 count
    count = int(input())

    # 初始化最小索引和为最大整数值
    min_idx_sum = float('inf')
    # 初始化答案为空字符串
    ans = ""

    # 使用两层循环遍历数组中的所有可能的组合
    for idx1 in range(len(steps)):
        for idx2 in range(idx1 + 1, len(steps)):
            # 获取两个步数
            step1 = steps[idx1]
            step2 = steps[idx2]

            # 如果两个步数之和等于 count
            if step1 + step2 == count:
                # 计算当前组合的索引和
                idx_sum = idx1 + idx2
                # 如果当前组合的索引和小于已找到的最小索引和
                if idx_sum < min_idx_sum:
                    # 更新最小索引和
                    min_idx_sum = idx_sum
                    # 更新答案
                    ans = [step1, step2]
                # 找到满足条件的组合后,跳出内层循环
                break

    # 输出结果
    print(ans)


if __name__ == "__main__":
    main()


12345678910111213141516171819202122232425262728293031323334353637383940

JavaScript

const readline = require('readline');

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

const inputLines = [];

// 读取输入行
rl.on('line', (line) => {
  inputLines.push(line);
  if (inputLines.length === 2) {
    rl.close();
  }
});

rl.on('close', () => {
  // 解析输入的步数数组和房子总格数
  const steps = JSON.parse(inputLines[0]);
  const count = parseInt(inputLines[1], 10);

  let minIdxSum = Number.MAX_VALUE;
  let ans = '';

  // 遍历数组中的所有可能的组合
  for (let idx1 = 0; idx1 < steps.length; idx1++) {
    for (let idx2 = idx1 + 1; idx2 < steps.length; idx2++) {
      const step1 = steps[idx1];
      const step2 = steps[idx2];

      // 如果两个步数之和等于房子总格数
      if (step1 + step2 === count) {
        const idxSum = idx1 + idx2;
        // 如果当前组合的索引和小于已找到的最小索引和
        if (idxSum < minIdxSum) {
          // 更新最小索引和
          minIdxSum = idxSum;
          // 更新答案
          ans = `[${step1}, ${step2}]`;
        }
        // 找到满足条件的组合后,跳出内层循环
        break;
      }
    }
  }

  // 输出结果
  console.log(ans);
});


12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152

C++

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <limits>
#include <algorithm>
using namespace std;
int main() {
    // 读取一行输入,将其转换为 int 数组 steps
    string tmp;
    getline(cin, tmp);
    tmp = tmp.substr(1, tmp.length() - 2);
    istringstream iss(tmp);
    vector<int> steps;
    string token;
    while (getline(iss, token, ',')) {
        steps.push_back(stoi(token));
    }

    // 读取房子总格数 count
    int count;
    cin >> count;

    // 初始化最小索引和为最大整数值
    int minIdxSum = numeric_limits<int>::max();
    // 初始化答案为空字符串
    string ans = "";

    // 使用两层循环遍历数组中的所有可能的组合
    for (size_t idx1 = 0; idx1 < steps.size(); idx1++) {
        for (size_t idx2 = idx1 + 1; idx2 < steps.size(); idx2++) {
            // 获取两个步数
            int step1 = steps[idx1];
            int step2 = steps[idx2];

            // 如果两个步数之和等于 count
            if (step1 + step2 == count) {
                // 计算当前组合的索引和
                int idxSum = static_cast<int>(idx1 + idx2);
                // 如果当前组合的索引和小于已找到的最小索引和
                if (idxSum < minIdxSum) {
                    // 更新最小索引和
                    minIdxSum = idxSum;
                    // 更新答案
                    ans = "[" + to_string(step1) + ", " + to_string(step2) + "]";
                }
                // 找到满足条件的组合后,跳出内层循环
                break;
            }
        }
    }

    // 输出结果
    cout << ans << endl;

    return 0;
}


1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859

C语言

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

int main() {
    // 定义一个字符数组用于存储输入的步数
    char tmp[10000];
    
    // 读取一行输入,将其存储在tmp中
    fgets(tmp, 10000, stdin);

    // 去掉首尾的方括号,并通过逗号分隔输入,转换为整数数组steps
    int steps[5000];  // 假设步数的最大长度不会超过5000
    int stepCount = 0; // 实际步数的数量
    char *token = strtok(tmp + 1, ","); // 从第二个字符开始(跳过左方括号)
    while (token != NULL && *token != ']') {
        steps[stepCount++] = atoi(token); // 将分割出的每个字符串转换为整数并存入steps数组
        token = strtok(NULL, ",");
    }

    // 读取房子总格数count
    int count;
    scanf("%d", &count);

    // 初始化最小索引和为最大整数值
    int minIdxSum = INT_MAX;
    // 初始化答案为两个步数的值
    int ans1 = 0, ans2 = 0;

    // 使用两层循环遍历数组中的所有可能的组合
    for (int idx1 = 0; idx1 < stepCount; idx1++) {
        for (int idx2 = idx1 + 1; idx2 < stepCount; idx2++) {
            // 获取两个步数
            int step1 = steps[idx1];
            int step2 = steps[idx2];

            // 如果两个步数之和等于count
            if (step1 + step2 == count) {
                // 计算当前组合的索引和
                int idxSum = idx1 + idx2;
                // 如果当前组合的索引和小于已找到的最小索引和
                if (idxSum < minIdxSum) {
                    // 更新最小索引和
                    minIdxSum = idxSum;
                    // 更新答案步数
                    ans1 = step1;
                    ans2 = step2;
                }
                // 找到满足条件的组合后,跳出内层循环
                break;
            }
        }
    }

    // 输出结果,格式为"[step1, step2]"
    printf("[%d, %d]\n", ans1, ans2);

    return 0;
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
python+opencv简谱识别音频生成系统源码含GUI界面+详细运行教程+数据 一、项目简介 提取简谱中的音乐信息,依据识别到的信息生成midi文件。 Extract music information from musical scores and generate a midi file according to it. 二、项目运行环境 python=3.11.1 第三方库依赖 opencv-python=4.7.0.68 numpy=1.24.1 可以使用命令 pip install -r requirements.txt 来安装所需的第三方库。 三、项目运行步骤 3.1 命令行运行 运行main.py。 输入简谱路径:支持图片或文件夹,相对路径或绝对路径都可以。 输入简谱主音:它通常在第一页的左上角“1=”之后。 输入简谱速度:即每钟拍数,同在左上角。 选择是否输出程序中间提示信息:请输入Y或N(不区大小写,下同)。 选择匹配精度:请输入L或M或H,对应低/中/高精度,一般而言输入L即可。 选择使用的线程数:一般与CPU核数相同即可。虽然python的线程不是真正的多线程,但仍能起到加速作用。 估算字符上下间距:这与简谱中符号的密集程度有关,一般来说纵向符号越稀疏,这个值需要设置得越大,范围通常在1.0-2.5。 二值化算法:使用全局阈值则跳过该选项即可,或者也可输入OTSU、采用大津二值化算法。 设置全局阈值:如果上面选择全局阈值则需要手动设置全局阈值,对于.\test.txt中所提样例,使用全局阈值并在后面设置为160即可。 手动调整中间结果:若输入Y/y,则在识别简谱后会暂停代码,并生成一份txt文件,在其中展示识别结果,此时用户可以通过修改这份txt文件来更正识别结果。 如果选择文件夹的话,还可以选择所选文件夹中不需要识别的文件以排除干扰
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值