牛客编程巅峰赛S1赛季

第一场:https://ac.nowcoder.com/acm/contest/6218
第二场:https://ac.nowcoder.com/acm/contest/6219
第三场:https://ac.nowcoder.com/acm/contest/6220
第四场:https://ac.nowcoder.com/acm/contest/6384
第五场:https://ac.nowcoder.com/acm/contest/6489

每次比赛都只会前两题

第1场 - 青铜&白银局

A - 移动字母

注意这道题卡时间复杂度,必须要 O ( n ) O(n) 才能过

class Solution {
public:
    /**
     * 
     * @param s string字符串 
     * @return string字符串
     */
    string change(string s) {
        // write code here
        string ns = "";
        int n = s.size(), cnt = 0;
        for (int i = 0; i < n; i++) {
            if (s[i] == 'a') {
                cnt++;
            } else {
                ns += s[i];
            }
        }
        while (cnt > 0) {
            ns += 'a';
            cnt--;
        }
        return ns;
    }
};

B - 魔法数字

bfs,因为最后的判断没有写好,导致一直WA

class Solution {
public:
    /**
     * 返回最后要输出的答案
     * @param n int整型 表示牛牛的数字
     * @param m int整型 表示牛妹的数字
     * @return int整型
     */
    int INF = 0x3f3f3f3f;
    int d[1500];
    queue<int> que;
    
    int solve(int n, int m) {
        // write code here
        memset(d, INF, sizeof d);
        que.push(n);
        d[n] = 0;
        
        while (que.size()) {
            int x = que.front(); que.pop();
            if (x == m) {
                return d[m];
            }
            int nx;
            for (int i = 0; i < 3; i++) {
                if (i == 0) {
                    nx = x + 1;
                } else if (i == 1) {
                    nx = x - 1;
                } else if (i == 2) {
                    nx = x * x;
                }
                if (nx > 0 && nx <= 1500 && d[nx] == INF) {
                    que.push(nx);
                    d[nx] = d[x] + 1;
                }
                
            }
        }
        
    }
};

C - 牛妹的春游

目前还不会,以后补上

第2场 - 青铜&白银

A - 牛牛扔牌

class Solution {
public:
    /**
     * 
     * @param x string字符串 字符串从前到后分别是从上到下排列的n张扑克牌
     * @return string字符串
     */
    string Orderofpoker(string x) {
        // write code here
        int n = x.size() / 2;
        char s, t;
        string res = ""; 
        while (n != 0) {
            if (n == 4 || n == 6 || n == 8 || n == 9 || n == 10) {
                s = x[x.size() - 2], t = x[x.size() - 1];
                res += s;
                res += t;
                x.erase(x.size() - 2, 2);
            } else {
                s = x[0], t = x[1];
                res += s;
                res += t;
                x.erase(0, 2);
            }
            
            n = x.size() / 2;
            
        }
        return res;
    }
};

B - 疯狂过山车

class Solution {
public:
    /**
     * 
     * @param n int整型 
     * @param num int整型vector 
     * @return int整型
     */
    int getMaxLength(int n, vector<int>& num) {
        // write code here
        int res = 0, cnt = 1;
        bool f1 = 0;

        for (int i = 0; i < n - 1; i++) {
        	// 上升阶段 
        	if (f1 == 0) {
        		if (num[i] < num[i + 1]) {
        			cnt++; 
        		} else if (num[i] == num[i + 1]) {
        			cnt = 1;
        		} else {
        			f1 = 1;
        			cnt++;
        			res = max(res, cnt);
        		}
        	// 下降阶段 
        	} else if (f1 == 1) {
        		if (num[i] > num[i + 1]) {
        			cnt++;
        			res = max(res, cnt);
        		} else if (num[i] == num[i + 1]) {
        			f1 = 0;
        			cnt = 1;
        		} else {
        			f1 = 0;
        			cnt = 2;
        		}
        	}
        }
        return res;
    }
};

C - 牛牛的棋盘

猜你喜欢

转载自blog.csdn.net/weixin_43772166/article/details/107239374