DG计算机视觉公司机试题

1.小A小B比数游戏

题目:
这里写图片描述
分析:
方法一:
给定区间1~n,判断小B取值在中位数的哪侧,也就是说,如果小B取值在中位数右侧,那小A只要取值为小B取值数-1,反之,如果小B取值在中位数左侧,那小A只要取值为小B取值数+1。更具体地说:
情况一:如果小B取值在中位数右侧,随机数的取值为1~n,所以如果小A只要取值为B-1,那么随机数分布在小B左侧的情况下,小A赢。(因为左侧明显多嘛)
反之。
方法二:
遍历小A可选的所有值,从中选出获胜概率最大的。
代码实现
方法一:

#include<iostream>
using namespace std;

int main() {
    int n, b;
    while (cin >> n >> b) {
        if (b > n / 2) cout << b - 1 << endl;
        else cout << b + 1 << endl;
    }
    return 0;
}

方法二:

#include<iostream>
using namespace std;

int main() {
    int n, b;
    while (cin >> n >> b) {
        int max_win = 0;
        int a = 0;
        for (int a_optinal = 1; a_optinal <= n; a_optinal++) {
            int win = 0;
            for (int ran = 1; ran <= n; ran++) {
                if (abs(ran - a_optinal) < abs(ran - b)) {
                    win++;
                }
            }
            if (win > max_win) {
                max_win = win;
                a = a_optinal;
            }
        }
        cout << a << endl;
    }
    return 0;
}

2.central sphere

题目:
A central sphere of radius R is the set of all points(x, y, z) in 3D space that satisfy the following equation:
x2+y2+z2=R2
We say that a set of central spheres covers a set points if each of the points belongs to one of the spheres.
Assume that the following declarations are given:

struct Point3D { 
int x; 
int y; 
int z; 
}; 

Write a function:
int solution(struct Point3D A[], int N);
that, given an array describing of a set of points in 3D space, returns the minimum number of central spheres required to cover them.
For example, given the following array:

A[0].x= 0, A[0].y= 5, A[0].z= 4; 
A[1].x= 0, A[1].y= 0, A[1].z=-3; 
A[2].x=-2, A[2].y= 1, A[2].z=-6; 
A[3].x= 1, A[3].y=-2, A[3].z= 2; 
A[4].x= 1, A[4].y= 1, A[4].z= 1; 
A[5].x= 4, A[5].y=-4, A[5].z= 3; 

the function should return 3, because three central spheres are required to cover these points:

  • a central sphere of radius sqrt(3) covers point number 4,
  • a central sphere of radius 3 covers point number 1 and 3,
  • a central sphere of radius sqrt(41) covers point number 0, 2 and 5.

It is impossible to cover these points with fewer central spheres.
Assume that:

  • N is an integer within the range [0…100,000];
  • the coordinates of each point in array A are integers within the range [-10,000…10,000];

Complexity:

  • expected worst-case time complexity is O(N*log(N));
  • expected worst-case space complexity is O(N), beyond input storage(not counting the storage required for input arguments).

Elements of input arrays can be modified.
分析:
中心球,所有球的球心都在座标系原点。变量只有一个半径R了。
代码实现

#include<iostream>
#include<set>
using namespace std;

struct Point3D {
    int x;
    int y;
    int z;
    Point3D(int px, int py, int pz) { x = px; y = py; z = pz; }
};

int solution(struct Point3D A[], int N) {
    set<long int> s;
    for (int i = 0; i < N; i++) {
        s.insert(A[i].x*A[i].x + A[i].y*A[i].y + A[i].z*A[i].z);
    }
    return s.size();
}

int main() {
    struct Point3D A[6] = { Point3D(0, 5, 4), Point3D(0, 0, -3),Point3D(-2, 1, -6),Point3D(1, -2, 2),Point3D(1, 1, 1),Point3D(4, -4, 3) };
    cout << solution(A, 6) << endl;
    return 0;
}

3.oscillating slice of array

题目:
A zero-indexed array A consisting of N integers is given. A pair of indices(P, Q), such that 0 <= P <= Q < N, is called a slice of array A. A slice is described as oscillating if:

  • every two consecutive elements of the array A inside the slice are different, and
  • no three consecutive elements of the array A inside the slice are monotonic.

More precisely, slice(P, Q) is alternating if:

  • A[P] > A[P+1] < A[P+2] > …, and so on, up to A[Q], or
  • A[P] < A[P+1] > A[P+2] <…, and so on, up to A[Q].

Note that if P=Q then the slice is alse osciliating.
Write a function:
int solution(int A[], int N).
that, given an array A consisting of N integers, returns the size of the largest oscillating slice in A. If there
is no such slice, the function should return 0.
For example, given array A such that:

    A[0]=5
    A[1]=0
    A[2]=-2
    A[3]=6
    A[4]=3
    A[5]=4
    A[6]=4
    A[7]=-3
    A[8]=5

the function should return 5, because:

  • (1, 5) is an oscillating slice of length 5(A[1] > A[2] < A[3] > A[4] < A[5]), and
  • all other oscillating slices in A are shorter.

Assume that:

  • N is an integer within the range [0…100,000];
  • each element of array A is an integer within the range
    [-1,000,000,000…1,000,000,000].

Complexity:

  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(1),beyond input storage(not counting the storage required for input arguments).

Elements of input arrays can be modified.
分析:
题目简单,弄清逻辑关系、判断即可。
代码实现

#include <iostream>
using namespace std;

int solution(int A[], int N) {
    int maxlen=1;
    int len=1;
    for(int i=1; i<N; i++) {
        if(A[i]==A[i-1]){
            if(maxlen<len) maxlen=len;
            len=1;
        }
        else if(len==1) {
            len++;
        }
        else if((A[i-1]-A[i-2])*(A[i-1]-A[i])>0) {
            len++;
        }
        else {
            if(maxlen<len) maxlen=len;
            len=2;
        }
    }
    if(maxlen<len) maxlen=len;
    return maxlen;
}

int main() {
    int A[9]={5, 0, -2, 6, 3, 4, 4, -3, 5};
    cout << solution(A, 9);
    return 0;
}

4.the area of the intersection of two circles

题目:
Write a function
double solution(int x1, int y1, int r1, int x2, int y2, int r2).
that, given six integers describing two circles C1 and C2, returns the area of the intersection of circles C1 and C2. The function should return 0.0 if the intersection is a point or is empty. The difference between the actual area of the intersection and the number returned by the function should be less than 0.0001.
Circles C1 and C2 are described as follows:

  • (x1, y1) are the co-ordinates of the center of circle C1;
  • r1 is the radius of circle C1;
  • (x2, y2) are the co-ordinates of the center of circle C2;
  • r2 is the radius of circle C2.

Assume that:

  • x1, y1, x2 and y2 are integers within the range [-200,000…200,000];
  • r1 and r2 are integers within the range [1…200,000].

For example, given x1=2, y1=2, r1=3, x2=5, y2=5 and r2=3, the function may return 5.1371, because the actual area of intersection of the circle with center (2, 2), radius 3 and the circle with center(5, 5), radius 3 is 5.137166941 +- 0.000000001 and the difference between this number and 5.1371 is less than 0.0001.
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
分析:
求重叠面积,参见:两圆相交求面积
代码实现

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

double solution(int x1, int y1, int r1, int x2, int y2, int r2) {
    double pi=3.1415926;
    double d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    if(r1+r2 <= d) return 0;
    else if(abs(r1-r2)>=d) return pi*min(r1, r2)*min(r1, r2);
    else {
        double a1=acos((r1*r1+d*d-r2*r2)/(2*r1*d));
        double a2=acos((r2*r2+d*d-r1*r1)/(2*r2*d));
        return a1*r1*r1+a2*r2*r2-r1*d*sin(a1);
    }
}

int main() {
    cout << solution(2, 2, 3, 5, 5, 3);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章