#P3085. 跳房子(100分)
          
                        
                                    
                      
        
              - 
          
          
                      1000ms
            
          
                      Tried: 204
            Accepted: 44
            Difficulty: 4
            
          
          
          
                       所属公司 : 
                              华为od
                                
            
                      
          
 
- 
                        算法标签>暴力枚举          
 
跳房子(100分)
字符串+(a+b)
从一个包含整数的字符串中解析出所有整数,可以利用函数也可以利用循环模拟(注意可能包含负数),枚举所有可能的整数对,找出其和等于给定的目标值,并且其索引和最小的那一对.
代码如下
cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 100005
string s;
int a[N];
int n;
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>s;
	for(int i=1;i<s.length()-1;i++){
		int j=i;
		int res=s[j]-'0';
		int boo=1;
		if(s[i]=='-'){
			res=s[j+1]-'0';
			boo=-1;
			j++;
		}
		while(s[j+1]>='0'&&s[j+1]<='9'&&j+1<=s.length()-1){
			j++;
			res=res*10+s[j]-'0';
		}
		i=j+1;
		res*=boo;
		a[++n]=res;
	}
	int sum;
	cin>>sum;
	int id=1e18;
	int x,y;
	string ans;
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){
			if(a[i]+a[j]==sum&&i+j<id){
				x=i,y=j;
				id=i+j;
			}
		}
	}
	cout<<"["<<a[x]<<", "<<a[y]<<"]";
	return 0;
}
python
s = input()
sum_val = int(input())
a = []
i = 1
while i < len(s) - 1:
    if s[i] == ',' or s[i] == ' ':  # 跳过逗号和空格
        i += 1
        continue
    res = 0
    boo = 1
    if s[i] == '-':  # 处理负数
        boo = -1
        i += 1
    while i < len(s) and '0' <= s[i] <= '9':  # 提取数字
        res = res * 10 + (int(s[i]) - int('0'))
        i += 1
    res *= boo
    a.append(res)
id_val = float('inf')
x, y = None, None
for i in range(len(a)):
    for j in range(i + 1, len(a)):
        if a[i] + a[j] == sum_val and i + j < id_val:
            x, y = i, j
            id_val = i + j
print(f"[{a[x]}, {a[y]}]")
java
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();  // 读取字符串
        long sum = sc.nextLong();  // 读取目标和
        List<Long> a = new ArrayList<>();
        int i = 1;
        while (i < s.length() - 1) {
            // 跳过逗号和空格
            if (s.charAt(i) == ',' || s.charAt(i) == ' ') {
                i++;
                continue;
            }
            long res = 0;
            long boo = 1;
            // 处理负数
            if (s.charAt(i) == '-') {
                boo = -1;
                i++;
            }
            // 提取数字
            while (i < s.length() && Character.isDigit(s.charAt(i))) {
                res = res * 10 + (s.charAt(i) - '0');
                i++;
            }
            res *= boo;  // 计算最终结果
            a.add(res);  // 将结果加入列表
        }
        long id = Long.MAX_VALUE;
        int x = -1, y = -1;
        // 寻找两个和为 sum 的数,并记录它们的位置
        for (int k = 0; k < a.size(); k++) {
            for (int l = k + 1; l < a.size(); l++) {
                if (a.get(k) + a.get(l) == sum && k + l < id) {
                    x = k;
                    y = l;
                    id = k + l;
                }
            }
        }
        // 输出结果
        System.out.println("[" + a.get(x) + ", " + a.get(y) + "]");
    }
}
        题目描述
跳房子,也叫跳飞机,是一种世界性的儿童游戏。
游戏参与者需要分多个回合按顺序跳到第1格直到房子的最后一格。
跳房子的过程中,可以向前跳,也可以向后跳。
假设房子的总格数是count,小红每回合可能连续跳的步教都放在数组steps中,请问数组中是否有一种步数的组合,可以让小红两个回合跳到量后一格?
如果有,请输出索引和最小的步数组合。
注意:
- 数组中的步数可以重复,但数组中的元素不能重复使用。
 - 提供的数据保证存在满足题目要求的组合,且索引和最小的步数组合是唯一的。
 
输入描述
第一行输入为每回合可能连续跳的步数,它是int整数数组类型。 第二行输入为房子总格数count,它是int整数类型。
输出描述
返回索引和最小的满足要求的步数组合(顺序保持steps中原有顺序)
备注
- count≤1000
 - 0≤steps.length≤5000
 - −100000000≤steps≤100000000
 
样例1
输入
[1,4,5,2,2]
7
输出
[5, 2]
样例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]