首页 > 奇安信软件开发 C/C++ 两道编程题全 AC
头像
已注销
发布于 2020-09-12 11:37
+ 关注

奇安信软件开发 C/C++ 两道编程题全 AC

题 1 代码:

class Solution
{
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * M包糖果,抛M次硬币,硬币连续n次为正面,最多能得到多少颗糖果
     * @param candies int整型vector 每包糖果的数量
     * @param coin int整型vector 抛硬币的结果
     * @param n int整型 连续是正面的次数
     * @return int整型
     */
    int maxCandies(vector<int>& candies, vector<int>& coin, int n)
    {
        int size = candies.size();
        int ans = 0;
        if (n >= size)
        {
            for (int i = 0; i < size; ++i) ans += candies[i];
            return ans;
        }
        for (int i = 0; i < size; ++i) if (coin[i] == 0) ans += candies[i];
        int L = 0, R = n - 1;
        int max = 0;
        while (R < size)
        {
            int temp = 0;
            for (int i = L; i <= R; ++i) if (coin[i] == 1) temp += candies[i];
            if (max < temp) max = temp;
            ++L;
            ++R;
        }
        return ans + max;
    }
};



题 2 代码:

class Solution
{
    struct Rect
    {
        int L, R, T, B;
        Rect(int l, int r, int t, int b) :L(l), R(r), T(t), B(b) {}
    };
public:
    /**
     * 获得剩余区域内的最大干净矩形面积
     * @param x1 int整型
     * @param y1 int整型
     * @param x2 int整型
     * @param y2 int整型
     * @param x3 int整型
     * @param y3 int整型
     * @param x4 int整型
     * @param y4 int整型
     * @return int整型
     */
    int getMaxArea(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
    {
        int L1, R1, T1, B1;
        int L2, R2, T2, B2;
        x1 < x2 ? (L1 = x1, R1 = x2) : (L1 = x2, R1 = x1);
        y1 < y2 ? (T1 = y1, B1 = y2) : (T1 = y2, B1 = y1);
        x3 < x4 ? (L2 = x3, R2 = x4) : (L2 = x4, R2 = x3);
        y3 < y4 ? (T2 = y3, B2 = y4) : (T2 = y4, B2 = y3);
        Dif1({ L1, R1, T1, B1 });
        int ans = 0;
        for (auto& val : vec)
        {
            auto temp = Dif2(val, { L2,R2,T2,B2 });
            if (temp > ans) ans = temp;
        }
        return ans;
    }

    int Dif2(Rect r1, Rect r2)
    {
        int max = 0;
        if (r1.L < r2.L)
        {
            int w = r2.L >= r1.R ? (r1.R - r1.L) : (r2.L - r1.L);
            int temp = w * (r1.B - r1.T);
            if (max < temp) max = temp;
        }
        if (r1.R > r2.R)
        {
            int w = r2.R <= r1.L ? (r1.R - r1.L) : (r1.R - r2.R);
            int temp = w * (r1.B - r1.T);
            if (max < temp) max = temp;
        }
        if (r1.T < r2.T)
        {
            int w = r2.T >= r1.B ? (r1.B - r1.T) : (r2.T - r1.T);
            int temp = w * (r1.R - r1.L);
            if (max < temp) max = temp;
        }
        if (r1.B > r2.B)
        {
            int w = r2.B <= r1.T ? (r1.B - r1.T) : (r1.B - r2.B);
            int temp = w * (r1.R - r1.L);
            if (max < temp) max = temp;
        }
        return max;
    }
    void Dif1(Rect r)
    {
        if (r.L != 0) vec.push_back({ 0,r.L,0,10 });
        if (r.R != 10) vec.push_back({ r.R,10,0,10 });
        if (r.T != 0) vec.push_back({ 0,10,0,r.T });
        if (r.B != 0) vec.push_back({ 0,10,r.B,10 });
    }

    vector<Rect> vec;
};

全部评论

(2) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

热门推荐