跳房子,也叫跳飞机,是一种世界性的儿童游戏。
游戏参与者需要分多个回合按顺序跳到第1格直到房子的最后一格。
跳房子的过程中,可以向前跳,也可以向后跳。
假设房子的总格数是 ,小红每回合可能连续跳的步教都放在数组 中,请问数组中是否有一种步数的组合,可以让小红两个回合跳到量后一格?
如果有,请输出索引和最小的步数组合。
注意:
数组中的步数可以重复,但数组中的元素不能重复使用。
提供的数据保证存在满足题目要求的组合,且索引和最小的步数组合是唯一的。
第一行输入为房子总格数 ,它是 int
整数类型。
第二行输入为每回合可能连续跳的步数,它是 int
整数数组类型。
返回索引和最小的满足要求的步数组合(顺序保持steps中原有顺序)
[1,4,5,2,2]
7
[5, 2]
[-1,2,4,9,6]
8
[-1, 9]
此样例有多种组合满足两回合跳到最后,譬如:[-1,9]
,[2,6]
,其中[-1,9]
的索引和为0+3=3
,[2,6]
的索和为1+4=5
,所以索引和最小的步数组合[-1,9]
#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>
using namespace std;
vector<int> solution(int count, vector<int> steps) {
int minIndexSum = INT_MAX;
vector<int> result(2);
for (int i = 0; i < steps.size(); i++) {
for (int j = i + 1; j < steps.size(); j++) {
if (steps[i] + steps[j] == count && i + j < minIndexSum) {
minIndexSum = i + j;
result[0] = steps[i];
result[1] = steps[j];
}
if (steps[i] + steps[j] > count) {
break;
}
}
}
return result;
}
int main() {
string line;
getline(cin, line);
stringstream ss(line);
vector<int> steps;
copy(istream_iterator<int>(ss), istream_iterator<int>(), back_inserter(steps));
int count;
cin >> count;
vector<int> res = solution(count, steps);
cout << '[' << res[0] << ',' << res[1] << ']' << endl;
return 0;
}
package com.amoscloud.nowcoder.refactor.t0251_0260;
import java.util.*;
/**
* Created with IntelliJ IDEA.
*
* @Author: Amos
* @E-mail: amos@amoscloud.com
* @Date: 2023/7/22
* @Time: 23:25
* @Description:
*/
public class Main0253 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
if (line == null || line.length() == 0) {
System.out.println("0");
return;
}
String[] strs = line.trim().substring(1, line.length() - 1).split(",");
int[] steps = new int[strs.length];
for (int i = 0; i < strs.length; i++) {
steps[i] = Integer.parseInt(strs[i]);
}
int count = sc.nextInt();
int[] res = solution(count, steps);
System.out.println(Arrays.toString(res));
}
private static int[] solution(int count, int[] steps) {
int minIndexSum = Integer.MAX_VALUE;
int[] result = new int[2];
for (int i = 0; i < steps.length; i++) {
for (int j = i + 1; j < steps.length; j++) {
if (steps[i] + steps[j] == count && i + j < minIndexSum) {
minIndexSum = i + j;
result[0] = steps[i];
result[1] = steps[j];
}
if (steps[i] + steps[j] > count) {
break;
}
}
}
return result;
}
}
def solution(count, steps):
min_index_sum = float('inf')
result = [0, 0]
for i in range(len(steps)):
for j in range(i + 1, len(steps)):
if steps[i] + steps[j] == count and i + j < min_index_sum:
min_index_sum = i + j
result = [steps[i], steps[j]]
if steps[i] + steps[j] > count:
break
return result
line = input()
steps = list(map(int, line[1:-1].split(',')))
count = int(input())
print(solution(count, steps))
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
let input = []
readline.on('line', (line) => {
input.push(line)
if(input.length === 2) {
let steps = input[0].slice(1, -1).split(',').map(Number)
let count = Number(input[1])
console.log(solution(count, steps))
readline.close()
}
})
function solution(count, steps) {
let minIndexSum = Infinity
let result = [0, 0]
for (let i = 0; i < steps.length; i++) {
for (let j = i + 1; j < steps.length; j++) {
if (steps[i] + steps[j] == count && i + j < minIndexSum) {
minIndexSum = i + j
result = [steps[i], steps[j]]
}
if (steps[i] + steps[j] > count) {
break
}
}
}
return result
}
package main
import (
"fmt"
"strings"
"strconv"
)
func solution(count int, steps []int) []int {
minIndexSum := 1<<31 - 1
result := make([]int, 2)
for i := 0; i < len(steps); i++ {
for j := i + 1; j < len(steps); j++ {
if steps[i] + steps[j] == count && i + j < minIndexSum {
minIndexSum = i + j
result[0] = steps[i]
result[1] = steps[j]
}
if steps[i] + steps[j] > count {
break
}
}
}
return result
}
func main() {
var line string
fmt.Scan(&line)
steps_str := strings.Split(line[1:len(line)-1], ",")
steps := make([]int, len(steps_str))
for i, s := range steps_str {
steps[i], _ = strconv.Atoi(s)
}
var count int
fmt.Scan(&count)
res := solution(count, steps)
fmt.Println(res)
}