zoukankan      html  css  js  c++  java
  • 北京理工大学复试上机--2014

    1、系统中有最近打开文件的记录,现用整数表示打开的文件名,且只
    显示最近3个打开的文件,输出文件序列.
    示例:
    输入:1输出:1
    输入:2输出:2, 1
    输入:3
    输出:3, 2, 1
    输入:4
    输出:4,3,2
    输入:1
    输出:1,4,3
    输入:4
    输出:1,4, 3
    输入:3
    输出:1,4,3
    #include <iostream>
    #include <vector>
    #include <map>
    using namespace std;
    int main() {
        int a[100], num[100] = {0}, n, i = 0, j;
        vector<int> v;
        map<int, int> m;
        while (scanf("%d", &n)) {
            if (n == 0) break;
            if (m[n] == 0) {
                v.push_back(n);
                m[n]++;
            }
            if (v.size() > 3) {
                m[v[0]] = 0;
                v.erase(v.begin());
            }
            for (i = v.size() - 1; i >= 0; i--) {
                cout << v[i];
                if (i > 0) cout << ",";
            }
        }
        return 0;
    }
    2、在第-题基础上,稍作改动,显示最新打开的文件.
    示例:
    输入:1
    输出:1
    输入:2
    输出:2,1
    输入:3
    输出:3,2,1
    输入:4
    输出:4,3,2
    输入:1输出:1,4,3
    输入:4
    输出:4,1,3
    输入:3
    输出:3,4,1
    #include <iostream>
    #include <vector>
    #include <map>
    using namespace std;
    int main() {
        int a[100], num[100] = {0}, n, i = 0, j;
        vector<int> v, vv;
        map<int, int> m;
        while (scanf("%d", &n)) {
            if (n == 0) break;
            if (m[n] == 0) {
                v.push_back(n);
                m[n]++;
                n = 0;
                if (v.size() > 3) {
                    m[v[0]] = 0;
                    v.erase(v.begin());
                }
            }
            else {
                for (i = v.size() - 1; i >= 0; i--) {
                    if (v[i] == n) {
                        v.erase(v.begin() + i);
                        v.push_back(n);
                    }
                }
            }
            for (i = v.size() - 1; i >= 0; i--) {
                cout << v[i];
                if (i > 0) cout << ",";
            }
            cout << endl;
        }
        return 0;
    }
    3、求广义表的深度(实际就是括号匹配),示例:输入(c,((d,e),f),h)
    输出:3
    #include <iostream>
    #include <vector>
    #include <map>
    using namespace std;
    int main() {
        string s;
        while (cin >> s) {
            int cnt = 0, i, l, maxn = 0;
            l = s.length();
            for (i = 0; i < l; i++) {
                if (s[i] == '(') {
                    cnt++;
                    if (cnt > maxn)
                        maxn = cnt;
                }
                if (s[i] == ')') cnt--;
            }
            cout << maxn << endl;
        }
        return 0;
    }

    PS: 总感觉这个第三题是不是没有这么简单啊? 扎实一点  踏实一点 

  • 相关阅读:
    LeetCode 109 Convert Sorted List to Binary Search Tree
    LeetCode 108 Convert Sorted Array to Binary Search Tree
    LeetCode 107. Binary Tree Level Order Traversal II
    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode 103 Binary Tree Zigzag Level Order Traversal
    LeetCode 102. Binary Tree Level Order Traversal
    LeetCode 104. Maximum Depth of Binary Tree
    接口和多态性
    C# 编码规范
  • 原文地址:https://www.cnblogs.com/ache/p/12571737.html
Copyright © 2011-2022 走看看