Who Gets the Catch?

  • + 0 comments

    i got all test right

    #include <math.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <limits.h>
    #include <stdbool.h>
    #include <iostream>
    
    int whoGetsTheCatch(int n, int x, int pInt[], int velocities[]);
    
    int main() {
        int n;
        int x;
        std::cin>>n>>x;
        int positions[n],velocities[n];
        int temp;
        for (int i = 0; i <n ; ++i) {
            std::cin>>temp;
            positions[i]=temp;
        }
    
        for (int i = 0; i <n ; ++i) {
            std::cin>>temp;
            velocities[i]=temp;
        }
        int result=whoGetsTheCatch(n,x,positions,velocities);
        std::cout<<result;
    
        return 0;
    }
    
    int whoGetsTheCatch(int n, int x, int pInt[], int velocities[]) {
        float min_so_far=INT16_MAX;
        int numCatchers=0;
        int catcher=-1;
        for (int i = 0; i <n ; ++i) {
            float temp=((float)abs(x-pInt[i]))/velocities[i];
            if (temp==min_so_far)
                numCatchers+=1;
    
            if (temp<min_so_far){
                min_so_far=temp;
                catcher=i;
                numCatchers=1;
            }
        }
    
        if (numCatchers==1)
            return catcher;
        return -1;
    }