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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章