美文网首页算法刷题笔记
刷题笔记 - December 2023

刷题笔记 - December 2023

作者: Du1in9 | 来源:发表于2024-01-03 20:02 被阅读0次

    1.DFS_example

    #include <iostream>
    #include <vector>
    #include <queue>
    
    using namespace std;
    
    vector<vector<int>> arrayList = {
        {1, 2}, {0, 3, 4}, {0, 5}, {1}, {1}, {2}
    };
    
    void dfs(int node, vector<bool>& visited) {
        visited[node] = true;
        cout << node << " ";
    
        for (int neighbor : arrayList[node]) {
            if (!visited[neighbor]) {
                dfs(neighbor, visited);
            }
        }
    }
    
    void bfs(int startNode, vector<bool>& visited) {
        queue<int> q;
        q.push(startNode);
        visited[startNode] = true;
    
        while (!q.empty()) {
            int currentNode = q.front();
            cout << currentNode << " ";
            q.pop();
    
            for (int neighbor : arrayList[currentNode]) {
                if (!visited[neighbor]) {
                    q.push(neighbor);
                    visited[neighbor] = true;
                }
            }
        }
    }
    
    int main() {
        int nodes = 6;
        vector<bool> visited1(nodes, false);
        cout << endl << "DFS: ";    dfs(0, visited1);
        vector<bool> visited2(nodes, false);
        cout << endl << "BFS: ";    bfs(0, visited2);
    
        return 0;
    }
    

    2.2023,知识点:搜索

    #include <iostream>
    
    using namespace std;
    
    bool exist2023(int x) {
        int x2[4] = {3, 2, 0, 2};
        int index = 0;
        while(x) {
            int temp = x % 10;
            if(temp == x2[index]) {
                index++;
                if(index == 4) {
                    return true;
                }
            }
            x = (x - temp) / 10;
        }
        return false;
    }
    
    int main() {
        int count = 0;
        for (int x = 12345678; x <= 98765432; x++) {
            if (exist2023(x)) {
                count++;
            }
        }
        cout << 98765432 - 12345678 + 1 - count << endl;
        return 0;
    }
    

    3.硬币兑换,知识点:数学,枚举

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main() {
        int n;
        cin >> n;
    //  注:兑换的两新硬币面值不能一样,且只统计旧硬币,巨坑
        vector<int> map(2 * n + 1, 0);
        int result = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = i + 1; j <= n; j++) {
                map[i + j] += i;
                if(map[i + j] > result) {
                    result = map[i + j];
                }
            }
        }
    //    for(int i = 1; i < 2 * n + 1; i++) {
    //      cout << "map[" << i << "] = " << map[i] << endl;
    //  }
        cout << result;
        return 0;
    }
    

    4.工作时长,知识点:字符串

    2022-11-17 10:20:18
    2022-05-06 20:04:02
    2022-06-24 08:38:10
    2022-06-15 21:35:55
    2022-01-18 02:44:16
    2022-10-31 18:20:23
    2022-12-20 07:17:03
    2022-06-22 02:32:46
    2022-07-08 00:14:08
    2022-04-06 19:54:59
    2022-10-04 15:11:49
    2022-07-18 23:37:09
    2022-03-16 22:11:59
    2022-06-13 22:50:14
    2022-12-12 18:40:44
    2022-05-16 15:38:03
    2022-01-20 17:53:05
    2022-11-14 13:44:48
    2022-04-28 10:03:21
    2022-06-14 10:24:23
    2022-12-29 20:19:07
    2022-10-14 20:12:59
    2022-03-30 16:41:27
    2022-01-05 21:10:52
    2022-09-06 22:23:17
    2022-01-21 07:58:27
    2022-02-07 22:41:33
    2022-12-22 07:34:07
    2022-01-07 13:36:54
    2022-08-26 18:42:51
    2022-05-23 23:58:19
    2022-12-27 11:20:36
    2022-05-11 01:20:03
    2022-02-08 01:54:27
    2022-10-24 16:45:39
    2022-02-18 23:10:14
    2022-11-23 17:50:14
    2022-06-02 22:51:41
    2022-05-23 22:53:10
    2022-07-08 12:12:00
    2022-05-24 16:48:45
    2022-07-20 00:45:05
    2022-10-07 23:59:05
    2022-10-06 19:59:36
    2022-06-15 23:04:21
    2022-02-22 09:05:36
    2022-12-01 20:14:39
    2022-04-08 07:22:16
    2022-10-27 19:55:20
    2022-09-28 19:58:37
    2022-09-12 16:27:24
    2022-12-15 21:51:56
    2022-12-14 14:50:32
    2022-01-31 02:35:06
    2022-07-06 14:54:49
    2022-03-22 10:23:41
    2022-01-19 14:56:25
    2022-06-17 20:43:01
    2022-02-02 22:03:49
    2022-12-28 23:48:57
    2022-09-27 23:30:16
    2022-02-17 23:24:14
    2022-10-25 21:54:59
    2022-10-10 05:55:46
    2022-04-12 23:14:21
    2022-07-11 14:20:06
    2022-05-11 02:25:41
    2022-05-31 18:49:19
    2022-06-07 16:56:46
    2022-12-08 21:28:26
    2022-01-06 06:07:45
    2022-06-01 17:37:30
    2022-09-15 23:57:34
    2022-07-01 22:34:51
    2022-07-20 22:28:24
    2022-08-04 01:32:52
    2022-06-29 11:52:59
    2022-06-01 20:49:20
    2022-02-03 06:17:23
    2022-02-01 06:46:12
    2022-03-01 07:42:39
    2022-11-10 21:30:10
    2022-03-30 15:35:09
    2022-08-11 15:23:50
    2022-07-14 05:37:42
    2022-01-14 20:08:09
    2022-08-24 17:22:42
    2022-02-23 05:35:26
    2022-09-28 17:10:44
    2022-01-11 16:09:26
    2022-09-05 22:47:06
    2022-06-28 21:35:22
    2022-11-21 20:42:51
    2022-09-05 13:58:45
    2022-10-04 05:18:27
    2022-10-26 17:51:11
    2022-05-12 21:03:45
    2022-06-08 23:56:29
    2022-05-20 00:22:53
    2022-05-03 09:34:11
    2022-06-21 02:25:49
    2022-01-21 10:09:52
    2022-08-19 00:29:57
    2022-05-06 20:16:50
    2022-08-19 20:31:16
    2022-04-20 07:17:25
    2022-09-26 21:20:46
    2022-05-09 07:12:27
    2022-01-26 07:47:50
    2022-02-24 03:42:14
    2022-08-03 08:11:26
    2022-12-26 11:30:06
    2022-12-30 23:30:20
    2022-07-25 23:10:21
    2022-05-19 19:31:26
    2022-08-30 07:53:57
    2022-08-05 19:50:58
    2022-09-19 22:15:35
    2022-05-16 10:06:14
    2022-09-01 22:54:44
    2022-07-25 23:30:21
    2022-11-07 08:42:13
    2022-05-27 22:25:16
    2022-02-21 15:13:20
    2022-01-10 14:45:51
    2022-11-07 13:05:15
    2022-04-18 00:21:06
    2022-02-01 03:36:17
    2022-08-15 18:08:32
    2022-09-16 13:50:10
    2022-04-28 14:31:12
    2022-07-05 19:38:29
    2022-11-17 18:32:38
    2022-02-16 15:35:01
    2022-08-03 21:11:44
    2022-06-27 10:17:27
    2022-08-26 07:20:04
    2022-12-21 05:08:44
    2022-05-09 03:57:36
    2022-02-17 22:47:28
    2022-01-04 20:00:27
    2022-11-28 20:06:50
    2022-03-23 20:59:39
    2022-08-18 08:11:47
    2022-04-11 16:00:18
    2022-08-25 12:17:36
    2022-05-13 14:36:27
    2022-10-28 03:32:08
    2022-10-11 21:31:29
    2022-01-28 13:55:06
    2022-06-02 19:18:41
    2022-02-02 23:27:27
    2022-04-25 18:09:27
    2022-11-08 02:41:26
    2022-07-26 21:02:36
    2022-04-07 17:49:34
    2022-12-05 15:39:35
    2022-05-02 17:56:14
    2022-09-02 05:31:37
    2022-09-20 13:12:42
    2022-04-21 15:08:30
    2022-02-10 06:24:22
    2022-05-03 08:53:26
    2022-03-11 17:04:41
    2022-10-21 14:06:05
    2022-07-06 16:15:22
    2022-11-11 23:15:09
    2022-06-10 23:04:28
    2022-05-25 17:46:58
    2022-08-22 15:22:19
    2022-05-05 01:04:03
    2022-04-06 16:53:30
    2022-11-16 12:44:08
    2022-02-18 23:10:34
    2022-07-13 23:17:48
    2022-04-13 07:00:35
    2022-08-29 06:33:43
    2022-12-13 17:16:07
    2022-11-11 22:41:45
    2022-04-15 10:56:49
    2022-04-29 22:27:34
    2022-05-12 23:04:33
    2022-01-27 15:52:38
    2022-05-04 07:12:02
    2022-03-24 09:04:19
    2022-03-18 04:38:06
    2022-11-18 22:00:08
    2022-03-21 12:23:03
    2022-12-05 05:14:48
    2022-11-23 22:04:33
    2022-11-02 21:41:39
    2022-01-04 17:48:39
    2022-08-09 12:17:22
    2022-11-09 20:03:05
    2022-06-21 09:13:41
    2022-09-09 10:11:37
    2022-02-23 12:28:54
    2022-04-11 00:48:34
    2022-11-08 22:55:01
    2022-10-18 16:04:48
    2022-04-15 16:11:27
    2022-08-10 16:19:47
    2022-07-13 23:04:08
    2022-05-31 16:51:45
    2022-11-10 22:16:33
    2022-11-09 18:23:34
    2022-12-21 06:10:59
    2022-04-29 21:03:33
    2022-01-24 06:46:29
    2022-08-12 01:41:30
    2022-12-16 20:28:21
    2022-09-08 23:46:13
    2022-07-14 06:25:46
    2022-02-25 19:03:53
    2022-05-30 11:54:09
    2022-07-21 03:37:23
    2022-09-08 08:39:02
    2022-12-12 14:02:37
    2022-12-30 14:08:11
    2022-09-19 23:09:26
    2022-09-30 23:56:03
    2022-10-25 09:50:34
    2022-10-18 22:41:19
    2022-12-16 21:32:30
    2022-10-21 15:19:48
    2022-01-17 12:10:17
    2022-09-07 23:57:44
    2022-07-12 14:54:28
    2022-06-06 17:43:08
    2022-06-16 23:14:28
    2022-11-22 02:34:32
    2022-03-24 11:05:21
    2022-01-14 20:33:05
    2022-12-06 02:59:27
    2022-05-30 04:55:04
    2022-01-03 06:58:15
    2022-12-26 06:49:08
    2022-06-08 23:20:33
    2022-01-28 22:51:46
    2022-07-07 19:29:31
    2022-12-07 23:52:34
    2022-11-29 18:47:44
    2022-10-14 16:58:55
    2022-03-31 19:00:22
    2022-02-22 20:00:17
    2022-06-28 20:16:58
    2022-03-29 04:46:42
    2022-08-31 22:08:12
    2022-07-22 21:02:17
    2022-01-25 11:30:09
    2022-04-13 08:47:31
    2022-05-17 20:13:18
    2022-12-07 23:49:21
    2022-06-09 17:41:54
    2022-07-12 19:42:30
    2022-03-10 02:51:01
    2022-04-14 11:48:12
    2022-12-01 20:50:43
    2022-04-26 09:44:39
    2022-04-27 18:57:56
    2022-12-06 15:40:49
    2022-04-22 14:02:50
    2022-12-09 21:14:10
    2022-09-01 17:15:27
    2022-07-19 15:08:12
    2022-06-20 01:25:14
    2022-12-02 09:56:07
    2022-04-20 20:59:03
    2022-11-15 23:55:10
    2022-03-31 05:58:30
    2022-09-12 10:15:09
    2022-03-16 23:19:39
    2022-11-29 13:49:07
    2022-11-15 23:21:55
    2022-02-03 12:07:05
    2022-05-24 20:50:42
    2022-05-18 15:14:45
    2022-08-15 17:28:56
    2022-04-01 05:40:28
    2022-07-11 17:18:24
    2022-10-24 15:17:25
    2022-08-16 22:19:53
    2022-06-23 22:41:33
    2022-08-23 23:17:36
    2022-02-08 13:15:42
    2022-12-14 00:48:53
    2022-07-05 03:03:07
    2022-06-13 19:58:57
    2022-04-04 23:51:46
    2022-07-27 04:41:44
    2022-06-10 08:42:44
    2022-03-03 13:19:50
    2022-12-27 10:45:08
    2022-08-23 20:53:37
    2022-08-09 07:09:53
    2022-01-10 21:43:49
    2022-06-09 17:25:59
    2022-05-10 19:41:48
    2022-04-14 17:37:08
    2022-11-22 16:42:47
    2022-10-11 14:29:32
    2022-07-26 10:03:54
    2022-07-15 14:03:19
    2022-06-03 19:14:30
    2022-06-22 18:41:04
    2022-09-30 23:56:42
    2022-06-27 04:47:30
    2022-05-02 14:30:50
    2022-07-01 23:01:34
    2022-09-29 23:08:38
    2022-08-30 12:49:31
    2022-10-20 23:08:14
    2022-06-06 03:25:31
    2022-01-13 00:04:29
    2022-08-02 22:38:40
    2022-08-22 17:43:12
    2022-03-23 23:09:09
    2022-01-19 16:21:50
    2022-03-07 19:26:31
    2022-09-14 20:06:57
    2022-08-04 22:28:11
    2022-11-04 10:31:26
    2022-11-28 22:06:25
    2022-03-29 06:06:41
    2022-08-08 18:26:38
    2022-08-05 03:07:57
    2022-03-04 03:09:46
    2022-10-07 22:13:24
    2022-02-28 18:00:10
    2022-02-14 23:51:28
    2022-05-05 09:25:23
    2022-11-14 14:12:10
    2022-08-12 20:56:31
    2022-08-11 12:43:08
    2022-06-23 17:17:58
    2022-04-21 17:19:35
    2022-03-08 19:48:25
    2022-01-13 01:17:04
    2022-03-17 22:16:12
    2022-11-02 20:25:33
    2022-07-07 23:42:55
    2022-05-19 21:04:11
    2022-12-23 07:36:37
    2022-06-16 14:44:26
    2022-02-11 23:11:26
    2022-12-08 23:00:49
    2022-01-11 19:39:56
    2022-12-29 17:06:27
    2022-02-09 23:34:38
    2022-07-18 16:18:41
    2022-03-14 15:56:03
    2022-12-23 04:18:49
    2022-02-04 22:37:42
    2022-09-14 13:51:03
    2022-08-01 19:57:34
    2022-11-24 18:30:16
    2022-09-27 21:02:35
    2022-07-29 15:55:58
    2022-04-05 09:22:33
    2022-09-22 21:49:32
    2022-04-08 16:51:51
    2022-11-30 16:07:44
    2022-08-17 23:08:37
    2022-11-03 23:27:53
    2022-02-10 10:06:18
    2022-05-13 11:32:06
    2022-11-16 23:39:27
    2022-05-25 03:53:40
    2022-10-03 13:01:51
    2022-04-19 23:37:41
    2022-10-31 16:00:56
    2022-06-03 17:10:15
    2022-06-14 13:49:05
    2022-11-01 03:42:05
    2022-09-09 01:17:05
    2022-07-15 21:11:31
    2022-08-18 21:40:31
    2022-07-04 18:52:43
    2022-04-26 05:41:33
    2022-12-15 20:24:29
    2022-07-04 00:29:07
    2022-09-29 21:05:55
    2022-01-06 11:40:43
    2022-04-25 22:38:51
    2022-05-26 22:25:32
    2022-03-09 07:20:24
    2022-12-28 23:45:37
    2022-12-02 16:31:45
    2022-01-17 09:43:04
    2022-03-02 03:46:38
    2022-03-21 18:42:52
    2022-02-15 23:09:59
    2022-10-06 17:04:12
    2022-08-16 22:04:44
    2022-02-25 17:39:13
    2022-08-02 03:01:20
    2022-12-19 06:46:43
    2022-10-20 23:47:31
    2022-07-29 12:10:41
    2022-01-26 14:13:20
    2022-04-27 16:14:03
    2022-09-20 22:20:09
    2022-03-18 10:50:49
    2022-10-10 06:49:10
    2022-06-29 11:57:21
    2022-07-21 09:58:57
    2022-10-05 23:09:45
    2022-11-03 23:05:58
    2022-03-08 11:36:43
    2022-02-21 13:42:37
    2022-03-28 01:39:13
    2022-12-09 00:47:56
    2022-05-27 22:00:52
    2022-04-01 11:57:46
    2022-06-30 12:35:35
    2022-03-15 21:38:06
    2022-08-24 10:20:28
    2022-09-22 22:48:48
    2022-10-19 08:46:54
    2022-08-01 18:33:43
    2022-07-28 22:05:30
    2022-03-04 07:47:00
    2022-12-13 15:08:34
    2022-10-17 23:14:50
    2022-01-03 13:02:19
    2022-01-12 22:00:21
    2022-11-25 01:27:01
    2022-11-24 17:12:42
    2022-10-12 01:44:19
    2022-12-19 11:07:02
    2022-03-03 04:48:36
    2022-03-09 09:12:23
    2022-11-25 22:21:16
    2022-06-20 13:36:50
    2022-03-11 02:49:48
    2022-08-29 08:11:52
    2022-07-22 20:16:15
    2022-05-10 23:17:08
    2022-10-03 04:41:26
    2022-02-09 18:28:29
    2022-01-31 16:50:12
    2022-02-04 20:52:13
    2022-07-27 08:17:23
    2022-01-18 19:08:53
    2022-03-07 10:20:34
    2022-04-18 00:31:55
    2022-09-23 23:49:43
    2022-04-22 20:24:36
    2022-03-10 18:13:50
    2022-01-07 01:43:16
    2022-05-04 19:24:13
    2022-06-24 02:52:50
    2022-09-16 06:09:31
    2022-08-10 12:35:55
    2022-09-13 22:58:20
    2022-11-18 19:53:14
    2022-02-16 12:03:55
    2022-03-22 11:40:39
    2022-01-24 11:10:02
    2022-09-23 23:56:40
    2022-08-17 22:20:17
    2022-10-13 19:53:21
    2022-04-12 08:31:58
    2022-07-19 18:34:37
    2022-06-30 13:37:08
    2022-12-20 21:57:11
    2022-01-20 14:16:09
    2022-10-26 20:27:36
    2022-09-21 09:07:50
    2022-06-17 21:39:15
    2022-03-17 19:32:36
    2022-02-11 19:14:03
    2022-01-27 18:52:20
    2022-03-01 05:13:09
    2022-09-02 07:14:28
    2022-11-30 21:38:26
    2022-01-12 22:52:54
    2022-05-26 20:00:44
    2022-11-21 19:24:37
    2022-03-15 12:35:12
    2022-02-15 23:32:00
    2022-11-04 03:06:18
    2022-10-13 11:55:17
    2022-10-05 23:38:34
    2022-09-07 23:57:18
    2022-09-06 20:03:39
    2022-07-28 22:04:41
    2022-10-27 17:09:22
    2022-03-02 05:39:46
    2022-05-20 06:38:44
    2022-04-07 16:52:47
    2022-08-25 21:41:12
    2022-04-19 03:18:23
    2022-05-18 20:20:44
    2022-03-28 10:27:27
    2022-01-05 12:44:08
    2022-10-12 08:40:57
    2022-09-15 23:57:51
    2022-05-17 12:22:16
    2022-09-26 23:05:46
    2022-09-13 22:10:41
    2022-01-25 08:12:01
    2022-08-31 22:33:52
    2022-10-17 23:44:04
    2022-04-04 01:28:09
    2022-04-05 14:04:55
    2022-02-07 17:15:36
    2022-09-21 02:48:17
    2022-02-28 21:18:25
    2022-12-22 12:05:26
    2022-03-25 10:55:54
    2022-10-19 00:02:26
    2022-08-08 20:35:06
    2022-02-24 01:21:40
    2022-03-14 18:04:57
    2022-06-07 22:55:57
    2022-10-28 18:22:59
    2022-11-01 09:57:12
    2022-02-14 22:26:15
    2022-03-25 16:08:18
    
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    string deleteZero(string s) {
        if(s.size() == 2 && s[0] == '0') {
            s.erase(0, 1);
        }
        return s;
    }
    
    int calculateTime(int h, int m, int s, int h2, int m2, int s2) {
        int time = 0;
        if(s2 < s) {
            m2--;
            if(m2 < m) {
                return 3600 * (h2 - 1 - h) + 60 * (m2 + 60 - m) + (s2 + 60 - s);
            } else {
                return 3600 * (h2 - h) + 60 * (m2 - m) + (s2 + 60 - s);
            }
        } else {
            if(m2 < m) {
                return 3600 * (h2 - 1 - h) + 60 * (m2 + 60 - m) + (s2 - s);
            } else {
                return 3600 * (h2 - h) + 60 * (m2 - m) + (s2 - s);
            }
        }
    }
    
    int main() {
        // work.txt为excel日期排序的文本,自定义格式hh:mm:ss
        ifstream file("work.txt");
        string line, line2, S;
        int h, h2, s, m, m2, s2;
        int totalTime = 0;
        while(getline(file, line) && getline(file, line2)) {
            S = line.substr(0, 2);  h = stoi(deleteZero(S));
            S = line.substr(3, 2);  m = stoi(deleteZero(S));
            S = line.substr(6, 2);  s = stoi(deleteZero(S));
            S = line2.substr(0, 2); h2 = stoi(deleteZero(S));
            S = line2.substr(3, 2); m2 = stoi(deleteZero(S));
            S = line2.substr(6, 2); s2 = stoi(deleteZero(S));
            cout << h << "\t" << m << "\t" << s << "\t" << h2 << "\t" << m2 << "\t" << s2;
            cout << "\t" << "time:" << "\t" << calculateTime(h, m, s, h2, m2, s2) << endl;
            totalTime += calculateTime(h, m, s, h2, m2, s2);
        }
        cout << totalTime;// 5101913
        file.close();
        return 0;
    }
    

    5.求和,知识点:数学

    #include <iostream>
    
    using namespace std;
    
    int main() {
        cout << (1LL + 20230408LL) * 20230408LL / 2 << endl;
        return 0;
    }
    

    6.日期统计,知识点:枚举,字符串

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main() {
        vector<int> Array(100, 0);
        int count = 0;
        for(int i = 0; i < 100; i++) {
            cin >> Array[i];
        }
        int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int temp[8] = {2, 0, 2, 3};
        // 枚举每一个月
        for(int i = 1; i <= 12; i++) {
            cout << "\nmonth:" << i << "\nday:";
            if(i <= 9) {
                temp[4] = 0;
                temp[5] = i; 
            } else {
                temp[4] = i / 10;
                temp[5] = i % 10;               
            }
            // 枚举每一天
            for(int j = 1; j <= day[i - 1]; j++) {
                cout << j << " ";
                if(j <= 9) {
                    temp[6] = 0;
                    temp[7] = j; 
                } else {
                    temp[6] = j / 10;
                    temp[7] = j % 10;               
                }
                // 暴力对比序列
                int index = 0;
                for(int k = 0; k < 100; k++) {
                    if(Array[k] == temp[index]) {
                        index++;
                    }
                    if(index == 8) {
                        count++;
                        goto end_loop;
                    }
                }
                end_loop: continue;
            }
        }
        cout << endl << count;
        return 0;
    }
    

    7.幸运数,知识点:枚举,数学

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main() {
        int count = 0;
        for(int i = 10; i <= 99; i++) {
            if(i % 10 == i / 10) {
    //          cout << i << endl;
                count++;
            }
        }
        for(int i = 1000; i <= 9999; i++) {
            int x1 = i / 1000;
            int x2 = i % 1000 / 100;
            int y1 = i % 100 / 10;
            int y2 = i % 10;
            if(x1 + x2 == y1 + y2) {
    //          cout << i << endl;
                count++;
            }
        }
        for(int i = 100000; i <= 999999; i++) {
            int x1 = i / 100000;
            int x2 = i % 100000 / 10000;
            int x3 = i % 10000 / 1000;
            int y1 = i % 1000 / 100;
            int y2 = i % 100 / 10;
            int y3 = i % 10;
            if(x1 + x2 + x3 == y1 + y2 + y3) {
    //          cout << i << endl;
                count++;
            }
        }
        for(int i = 10000000; i <= 99999999; i++) {
            int x1 = i / 10000000;
            int x2 = i % 10000000 / 1000000;
            int x3 = i % 1000000 / 100000;
            int x4 = i % 100000 / 10000;
            int y1 = i % 10000 / 1000;
            int y2 = i % 1000 / 100;
            int y3 = i % 100 / 10;
            int y4 = i % 10;
            if(x1 + x2 + x3 + x4 == y1 + y2 + y3 + y4) {
    //          cout << i << endl;
                count++;
            }
        }
        cout << count;
        return 0;
    }
    

    8.三元组中心问题,知识点:枚举

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main() {
        int n;
        cin >> n;
        vector<int> Array(n, 0);
        vector<int> flag(n, 0);
        for(int i = 0; i < n; i++) {
            cin >> Array[i];
        }
        for(int i = 0; i < n - 2; i++) {
            for(int j = i + 1; j < n - 1; j++) {
                for(int k = j + 1; k < n; k++) {
                    if(Array[i] < Array[j] && Array[j] < Array[k]) {
    //                  cout << Array[i] << " " << Array[j] << " " << Array[k] << endl;
                        flag[j] = 1;
                    }
                }
            }
        }
        int count = 0;
        for(int i = 0; i < n; i++) {
            if(flag[i] == 1) {
                count++;
            }
        }
        cout << count << endl;
        return 0;
    }
    

    9.等腰三角形,知识点:模拟,字符串

    #include <iostream>
    #include <vector>
    #include <sstream>
    
    using namespace std;
    
    int main() {
        int n;
        cin >> n;
        // 生成递增字符
        string s;
        ostringstream S;
        for(int i = 1; i <= 4 * (n - 1); i++) {
            S << i;
        }
        s = S.str();
        // 初始化三角形
        vector<vector<char>> Array(n, vector<char>(2 * n - 1, 0));
        for(int i = 0; i < n - 1; i++) {
            for(int j = 0; j < n + i - 1; j++) {
                Array[i][j] = '.';
            }
        }
        int index = 0;
        // 填充左边
        for(int i = 0; i < n - 1; i++) {
            Array[i][n - i - 1] = s[index++];
        }
        // 填充底边
        for(int i = 0; i < 2 * n - 1; i++) {
            Array[n - 1][i] = s[index++];
        }
        // 填充右边
        for(int i = n - 2; i > 0; i--) {
            Array[i][n + i - 1] = s[index++];
        }
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n + i; j++) {
                cout << Array[i][j];
            }
            cout << endl;
        }
        return 0;
    }
    

    12.人物相关性分析,知识点:字符串,模拟

    #include <iostream>
    
    using namespace std;
    
    int main() {
        int K;
        string s;
        cin >> K;
        cin.ignore();
        getline(cin, s);
        // 标记两个人物的下标
        int index = -1, index2 = -1, count = 0;
        for(int i = 0; i < s.size(); i++) {
            if(s.substr(i, 5) == "Alice") {
                index = i;
                if(index2 > -1 && index - index2 - 3 <= K) {
                    count++;
                }
            }
            if(s.substr(i, 3) == "Bob") {
                index2 = i;
                if(index > -1 && index2 - index - 5 <= K) {
                    count++;
                }
            }
        }
        cout << count;
        return 0;
    }
    

    13.子串分值,知识点:字符串,枚举

    #include <iostream>
    
    using namespace std;
    
    int f(string s) {
        int map[26] = {0};
        for(char c : s) {
            map[c - 'a']++;
        }
        int count = 0;
        for(int i : map) {
            if(i == 1) {
                count++;
            }
        }
        return count;
    }
    
    int main() {
        string s;
        getline(cin, s);
        int total = 0;
        // 遍历所有子串
        for (int i = 0; i < s.size(); i++) {
            for (int j = 1; j <= s.size() - i; j++) {
                cout << s.substr(i, j) << "\tf = " << f(s.substr(i, j)) << endl;
                total += f(s.substr(i, j));
            }
        }
        cout << total;
        return 0;
    }
    

    14.音节判断,知识点:模拟,字符串

    #include <iostream>
    
    using namespace std;
    
    int main() {
        string s;
        cin >> s;
        int flag = 1, count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (flag == 1 && s[i] != 'a' && s[i] != 'e' && s[i] != 'i' && s[i] != 'o' && s[i] != 'u') {
                count++;
                flag = 0;
            } else if (flag == 0 && (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')) {
                count++;
                flag = 1;
            }
        }
        if (count == 4) {
            cout << "yes";
        } else {
            cout << "no";
        }
        return 0;
    }
    

    15.外卖店优先级,知识点:模拟,数据结构

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main() {
        int N, M, T;
        cin >> N >> M >> T;
        // N个商家, T天订单,优先缓存
        vector<vector<int>> order(N, vector<int> (T + 1, 0));
        vector<int> prior(N, 0);
        int ts, id;
        for(int i = 0; i < M; i++) {
            cin >> ts >> id;
            if(ts <= T) {
                order[id - 1][ts - 1]++;
            }
        }
        for(int i = 0; i < T; i++) {
            for(int j = 0; j < N; j++) {
                if(order[j][i] > 0) {
                    order[j][T] += order[j][i] * 2;
                } else {
                    order[j][T] = max(0, order[j][T] - 1);
                }
                if(order[j][T] > 5) {
                    prior[j] = 1;
                } else if (order[j][T] <= 3) {
                    prior[j] = 0;
                }
            }
        }
    //  for(int i = 0; i < N; i++) {
    //      for(int j = 0; j < T + 1; j++) {
    //          cout << order[i][j] << " ";
    //      }
    //      if(prior[i]) {
    //          cout << "\tyes";
    //      } else {
    //          cout << "\tno";
    //      }
    //      cout << endl;
    //  }
        int count = 0;
        for(int i = 0; i < N; i++) {
            if(prior[i] == 1) {
                count++;
            }
        }
        cout << count;
        return 0;
    }
    

    16.特别数的和,知识点:模拟,数学

    #include <iostream>
    
    using namespace std;
    
    bool goodNumber(int x) {
        int y;
        while(x) {
            y = x % 10;
            if(y == 2 || y == 0 || y == 1 || y == 9) {
                return true;
            }
            x = (x - y) / 10;
        }
        return false;
    }
    
    int main() {
        int n, result = 0;
        cin >> n;
        for(int i = 1; i <= n; i++) {
            if(goodNumber(i)) {
    //          cout << i << " ";
                result += i;
            }
        }
        cout << result;
        return 0;
    }
    

    17.清理水域,知识点:模拟,数据结构

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main() {
        int n, m, t;
        int r1, c1, r2, c2;
        cin >> n >> m >> t;
        vector<vector<int>> water(n, vector<int> (m, 0));
        for(int i = 0; i < t; i++) {
            cin >> r1 >> c1 >> r2 >> c2;
            for(int j = r1 - 1; j < r2; j++) {
                for(int k = c1 - 1; k < c2; k++) {
                    water[j][k] = 1;
                }
            }
        }
        int count = 0;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(water[i][j] == 1) {
                    count++;
                }
    //          cout << water[i][j] << " ";
            }
    //      cout << endl;
        }
        cout << n * m - count;
        return 0;
    }
    

    19.大乘积,知识点:排序,枚举

    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    bool smaller(int x, int y) {
        return x > y;
    }
    
    int main() {
        int count = 0;
        int number[30] = {
            99, 22, 51, 63, 72, 61, 20, 88, 40, 21,
            63, 30, 11, 18, 99, 12, 93, 16, 7, 53,
            64, 9, 28, 84, 34, 96, 52, 82, 51, 77
        };
        sort(number, number + 30, smaller);
        for(int i = 0; i < 29; i++) {
            if(number[i] * number[i + 1] < 2022) {
                break;
            }
            for(int j = i + 1; j < 30; j++) {
                if(number[i] * number[j] < 2022) {
                    break;
                } else {
    //              cout << number[i] << " * " << number[j] << " = " << number[i] * number[j] << endl;
                    count++;
                }
            }
        }
        cout << count;
        return 0;
    }
    

    20.龙虎斗,知识点:模拟,数学

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int n, m, p1, p2, s1, s2;
    
    // 计算龙虎势力差
    int HP(vector<int> game, int p2) {
        game[p2 - 1] += s2;
        int dragon = 0, tiger = 0;
        for(int i = 0; i < m - 1; i++) {
            dragon += game[i] * (m - 1 - i);
        }
        for(int i = n - 1; i >= m; i--) {
            tiger += game[i] * (i - m + 1);
        }
        return abs(dragon - tiger);
    }
    
    int main() {
        cin >> n;
        vector<int> game(n, 0);
        for(int i = 0; i < n; i++) {
            cin >> game[i];
        }
        cin >> m >> p1 >> s1 >> s2;
        game[p1 - 1] += s1;
        // 计算空投兵营
        int minHP = 9999;
        for(int i = 1; i <= n; i++) {
            if(HP(game, i) < minHP) {
                minHP = HP(game, i);
                p2 = i;
            }
        }
        cout << p2;
        return 0;
    }
    

    相关文章

      网友评论

        本文标题:刷题笔记 - December 2023

        本文链接:https://www.haomeiwen.com/subject/aikvndtx.html