algorithm
  • README
  • 算法题目
    • Page 1
  • 算法基础
    • 堆(大/小顶堆 、优先队列)
      • swift
      • js
      • C++
    • 哈希表(HashTable)
      • 线性探测
        • 稠密哈希表
        • 密集探测哈希表vs线性探测哈希表
      • C拉链法
        • 拉链法是否需要根据负载因子扩容?
      • set的实现
      • map的实现
    • 二叉树
      • 类型
        • AVL树和红黑树的关系与区别比较
      • 存储方式
      • 遍历
        • 前中后序遍历(深度)
          • 递归方式
          • 迭代法
        • 层次遍历(广度)
      • 二叉搜索树
        • 基本概念
        • 代码实现
      • AVL树
        • 基本概念
        • 代码实现
        • 疑问
          • AVL为什么LL的时候右转呢?
          • AVL树中的LR的情况
          • AVL树的调整
          • 删除节点的逻辑
      • 红黑树
        • 基本概念
        • 代码实现
        • 疑问
          • 红黑树和AVL的关系与区别
          • 红黑树和哈希表的关系与区别
          • 为什么有红黑树,比AVL优势在哪里?
    • 运算符
      • 交换两个数(不用临时变量)
  • leetcode结构
    • Javascript
      • 优先队列
    • C++
  • 公司面试题目
    • 汇丰银行
      • 跑步同一个脚印问题
      • 销售系统问题
Powered by GitBook
On this page
  1. 公司面试题目
  2. 汇丰银行

跑步同一个脚印问题

Martin’s father goes for a jog every morning. martin follows him several minutes later. his father starts at a position that is x1 meters away from their home and runs rectilinearly at a constant speed of v1 meters per step for n steps.
martin is standing at x2 meters away from his home. he wonders how fast he must run at some constant speed of v2 meters per step so as to maximize f, where f equals the number of his father's footsteps that martin will land on during his run. 
note that if more than one prospective velocity results in the same number of maximum common steps, output the highest prospective velocity as v2.
write an algorithm to help martin calculate f and v2.
input
the first line of the input consists of two space-separated integers - x1 and x2 representing the initial positions of martin’s father and martin, respectively.
the second line consists of two space-separated integers - v1 and n representing the velocity of the father and the number of steps taken by the father, respectively.
output
print two space-separated integers as maximum number of common footsteps f and respective speed v2. constraints
1 = x1 = 105
0 = x2 = x1
1 = v1 = 104
1 = n = 104
example
input:
3 2
2 20
output:
21 1
explanation:
martin can have a maximum of 21 common footsteps with a velocity of 1 m/step.

这里是问题的中文翻译:

---

马丁的父亲每天早上去慢跑。马丁在几分钟后跟着他的父亲出发。他的父亲从离家x1米的一个位置开始跑步,以每步v1米的速度直线奔跑,跑了n步。  
马丁站在离家x2米的位置。他想知道,他应该以每步v2米的速度跑步,以便尽可能多地踩在他父亲跑步的脚印上。已知马丁的第一个步伐将与他父亲的某一个步伐重合。

需要注意的是,如果多个潜在的速度v2可以使马丁踩在相同数量的父亲的脚印上,则输出最大的速度v2。

### 任务:
编写一个算法,帮助马丁计算他可以踩在多少个父亲的脚印上(f),以及对应的最优速度v2。

### 输入格式:
第一行输入包含两个用空格分隔的整数x1和x2,分别表示马丁的父亲和马丁的初始位置。  
第二行包含两个用空格分隔的整数v1和n,分别表示父亲的速度和父亲跑的步数。

### 输出格式:
输出两个用空格分隔的整数,表示最大数量的共同脚步数f以及对应的速度v2。

### 约束条件:
1 ≤ x1 ≤ 105  
0 ≤ x2 ≤ x1  
1 ≤ v1 ≤ 104  
1 ≤ n ≤ 104

### 输入示例:
### 约束条件:
1 ≤ x1 ≤ 105  
0 ≤ x2 ≤ x1  
1 ≤ v1 ≤ 104  
1 ≤ n ≤ 104

### 输入示例:
3 2 
2 20

### 输出示例:
21 1


#include <iostream>
#include <vector>

using namespace std;

vector<int> commonFootSteps(int fatherPos, int martinPos, int velFather, int steps) {
    vector<int> answer(2, 0);
    vector<int> temp(steps + 1, 0);

    for (int i = 0; i <= steps; i++) {
        temp[i] = fatherPos + velFather * i - martinPos;
    }

    for (int i = 0; i <= steps; i++) {
        if (temp[i] < 0)
            continue;

        int v2 = temp[i];
        int count = 0;

        for (int j = i; j <= steps; j++) {
            if (temp[j] % v2 == 0)
                count++;
        }

        if (answer[0] <= count) {
            answer[0] = count;
            answer[1] = v2;
        }
    }

    return answer;
}

int main() {
    int x1, x2, v, n;
    cin >> x1 >> x2 >> v >> n;
    vector<int> result = commonFootSteps(x1, x2, v, n);

    for (int i : result)
        cout << i << " ";

    return 0;
}
Previous汇丰银行Next销售系统问题

Last updated 8 months ago