算法设计_综合练习_编程题(下)

简介: 算法设计_综合练习_编程题

7-35 h0154.加勒比海盗船——最优装载问题


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1000005;
int t;
int main() {
    cin >> t;
    while (t --) {
        double c;
        vector<double> a;
        int m, cnt = 0;
        cin >> c >> m;
        for (int i = 0; i < m; i ++) {
            double x;
            cin >> x;
            a.push_back(x);
        }
        sort(a.begin(), a.end());
        double ans = 0;
        for (int i = 0; i < m; i ++) {
            ans += a[i];
            if (ans <= c) cnt ++;
            else break;
        }
        cout << cnt << endl;
    }
    return 0;
}


7-36 会场安排问题


#include <iostream>
#include <algorithm>
using namespace std;
struct activity {
    int b, e;
//     bool operator < (activity & t) {
//         if (e != t.e) return e < t.e;
//     }
}a[10010];
int n;
int st[10010];
bool cmp (activity x, activity y) {
    return x.b < y.b;
//     if (x.e != y.e) return x.e < y.e;
//     else return x.b < y.b;
}
int main() {
   cin >> n;
    int cnt = 0;
    for (int i = 0; i < n; i ++) {
        cin >> a[i].b >> a[i].e;
//         cout << a[i].b << ' ' << a[i].e << endl;
    }
    sort(a, a + n, cmp);
    for (int i = 0; i < n; i ++)
//         cout << a[i].b << ' ' << a[i].e << endl;
        if (!st[a[i].e]) {
            cnt ++;
            st[a[i].e] = 1;
            int pre = a[i].e;
            for (int j = i + 1; j < n; j ++)
                if (!st[a[j].e] && a[j].b >= pre) {
                    pre = a[j].e;
                    st[a[j].e] = 1;
                }
        }
    cout << cnt << endl;
    return 0;
}


7-37 h0145. 会议安排


#include <iostream>
#include <algorithm>
using namespace std;
struct activity {
    int b, e, no;
    bool operator < (activity & t) {
        if (e != t.e) return e < t.e;
        else return b < t.b;
    }
}a[10010];
int n, x ,y;
// bool cmp (activity x, activity y) {
//     if (x.b != x.b) return x.b < y.b;
//     else return x.e < y.e;
// }
int main() {
    int t;
    cin >> t;
    while (t --) {
        cin >> n;
        int cnt = 0, pre = 0;
        for (int i = 0; i < n; i ++) {
            cin >> a[i].b >> a[i].e;
    //         cout << a[i].b << ' ' << a[i].e << endl;
        }
        sort(a, a + n);
        for (int i = 0; i < n; i ++) {
//             cout << a[i].b << ' ' << a[i].e << endl;
            if (a[i].b >= pre) {
                pre = a[i].e;
//                 cout << a[i].b << ' ' << a[i].e << endl;
                cnt ++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}


7-38 最少失约


#include <iostream>
#include <algorithm>
using namespace std;
struct activity {
    int b, e, no;
    bool operator < (activity & t) {
        if (e != t.e) return e < t.e;
        else return b < t.b;
    }
}a[10010];
int n, x ,y;
// bool cmp (activity x, activity y) {
//     if (x.b != x.b) return x.b < y.b;
//     else return x.e < y.e;
// }
int main() {
    int t;
    cin >> t;
    while (t --) {
        cin >> n;
        int cnt = 0, pre = 0;
        for (int i = 0; i < n; i ++) {
            cin >> a[i].b >> a[i].e;
    //         cout << a[i].b << ' ' << a[i].e << endl;
        }
        sort(a, a + n);
        for (int i = 0; i < n; i ++) {
//             cout << a[i].b << ' ' << a[i].e << endl;
            if (a[i].b >= pre) {
                pre = a[i].e;
//                 cout << a[i].b << ' ' << a[i].e << endl;
                cnt ++;
            }
        }
        cout << n - cnt << endl;
    }
    return 0;
}


7-39 活动选择问题


#include <iostream>
#include <algorithm>
using namespace std;
struct activity {
    int b, e, no;
    bool operator < (activity & t) {
        if (e != t.e) return e < t.e;
        else return b < t.b;
    }
}a[10010];
int n;
// bool cmp (activity x, activity y) {
//     if (x.b != x.b) return x.b < y.b;
//     else return x.e < y.e;
// }
int main() {
    while (cin >> n) {
        int cnt = 0, pre = 0;
        for (int i = 0; i < n; i ++) {
            cin >> a[i].b >> a[i].e;
    //         cout << a[i].b << ' ' << a[i].e << endl;
        }
        sort(a, a + n);
        for (int i = 0; i < n; i ++) {
//             cout << a[i].b << ' ' << a[i].e << endl;
            if (a[i].b >= pre) {
                pre = a[i].e;
//                 cout << a[i].b << ' ' << a[i].e << endl;
                cnt ++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}


7-40 删数问题


// 贪心
#include <iostream>
using namespace std;
int k;
string str;
int main() {
    cin >> str >> k;
    for (int i = 0; i < k; i ++)
        for (int j = 0; j < str.size(); j ++)
            if (str[j] > str[j + 1]) {
                str.erase(str.begin() + j);
                break;
            }
    cout << str << endl;
    return 0;
}

7-41 最短路径条数

#include <iostream>
using namespace std;
const int N = 1010;
int g[N][N];
int main() {
    int n, m, x1, y1, x2, y2;
    cin >> n >> m >> x1 >> y1 >> x2 >> y2;
    if (x1 >= x2) swap(x1, x2);
    if (y1 >= y2) swap(y1, y2);
    for (int i = x1; i <= n; i ++) {
        for (int j = y1; j <= m; j ++) {
            if (x1 == i || y1 == i)
                g[i][j] = 1;
            else
                g[i][j] = g[i - 1][j] + g[i][j - 1];
        }
    }
    cout << g[x2][y2] << endl;
    return 0;
}


7-42 一步两步


#include <iostream>
using namespace std;
const int N = 1010;
int f[N], n;
int main() {
    cin >> n;
    f[0] = 1;
    f[1] = 2;
    for (int i = 2; i < n; i ++)
        f[i] = f[i - 1] + f[i - 2];
    cout << f[n - 1];
    return 0;
}


7-43 青蛙跳台阶


#include <iostream>
using namespace std;
const int N = 1010;
int f[N], n;
int main() {
    cin >> n;
    f[0] = 1;
    f[1] = 2;
    for (int i = 2; i < 60; i ++)
        f[i] = f[i - 1] + f[i - 2];
    while (n --) {
        int x;
        cin >> x;
        cout << f[x - 1] << endl;
    }
    return 0;
}


7-45 让人头疼的“双十一”


#include <iostream>
#include <cstring>
using namespace std;
const int N = 3010;
int m ,n;
int v[N], w[N];
int f[N];
int main() {
    int t;
    cin >> t;
    for (int k = 1; k <= t; k ++) {
        cin >> m >> n;
        for (int i = 1; i <= n; i ++)
            cin >> v[i];
        for (int i = 1; i <= n; i ++)
            cin >> w[i];
        for (int i = 1; i <= n; i ++)
            for (int j = m; j >= v[i]; j --)
                f[j] = max(f[j], f[j - v[i]] + w[i]);
        printf("Case #%d: %d\n", k, f[m]);
        memset(f, 0, sizeof f);
    }
    return 0;
}


7-46 h0173. 01背包问题


#include <iostream>
using namespace std;
const int N = 1010;
int n, m;
int v[N], w[N];
int f[N];
int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i ++)
        cin >> v[i + 1] >> w[i + 1];
    for (int i = 1; i <= n; i ++)
        for (int j = m; j >= v[i]; j --)
            f[j] = max(f[j], f[j - v[i]] + w[i]);
    cout << f[m] << endl;
    return 0;
}


7-47 最长公共子序列长度


#include <iostream>
using namespace std;
const int N = 1010;
string str1, str2;
int f[N][N];
int main() {
    cin >> str1 >> str2;
    for (int i = 1; i <= str1.size(); i ++) {
        for (int j = 1; j <= str2.size(); j ++) {
            if (str1[i - 1] == str2[j - 1])
                f[i][j] = f[i - 1][j - 1] + 1;
            else
                f[i][j] = max(f[i - 1][j], f[i][j - 1]);
        }
    }
    cout << f[str1.size()][str2.size()] << endl;
    return 0;
}


7-48 h0215.闭区间问题


#include <iostream>
#include <algorithm>
using namespace std;
struct activity {
    int b, e, no;
//     bool operator < (activity & t) {
//         if (e != t.e) return e < t.e;
//     }
}a[1010];
int n, x ,y;
bool cmp (activity x, activity y) {
    return x.e < y.e;
//     if (x.e != y.e) return x.e < y.e;
//     else return x.b < y.b;
}
int main() {
   cin >> n;
    int cnt = 0, pre = 0;
    for (int i = 0; i < n; i ++) {
        cin >> a[i].b >> a[i].e;
        if (a[i].b > a[i].e) {
            int t = a[i].e;
            a[i].e = a[i].b;
            a[i].b = t;
        }
    }
    sort(a, a + n, cmp);
    for (int i = 0; i < n; i ++) {
//         cout << a[i].b << ' ' << a[i].e << endl;
        if (a[i].b > pre) {
            pre = a[i].e;
//             cout << a[i].b << ' ' << a[i].e << endl;
            cnt ++;
        }
    }
    cout << n - cnt;
    return 0;
}


7-49 h0206. 区间选点


#include <iostream>
#include <algorithm>
using namespace std;
struct activity {
    int b, e, no;
//     bool operator < (activity & t) {
//         if (e != t.e) return e < t.e;
//     }
}a[1010];
int n, x ,y;
bool cmp (activity x, activity y) {
    return x.e < y.e;
//     if (x.e != y.e) return x.e < y.e;
//     else return x.b < y.b;
}
int main() {
   cin >> n;
    int cnt = 0, pre = -0x3f3f3f3f;
    for (int i = 0; i < n; i ++) {
        cin >> a[i].b >> a[i].e;
//         if (a[i].b > a[i].e) {
//             int t = a[i].e;
//             a[i].e = a[i].b;
//             a[i].b = t;
//         }
    }
    sort(a, a + n, cmp);
    for (int i = 0; i < n; i ++) {
//         cout << a[i].b << ' ' << a[i].e << endl;
        if (a[i].b > pre) {
            pre = a[i].e;
//             cout << a[i].b << ' ' << a[i].e << endl;
            cnt ++;
        }
    }
    cout << cnt;
    return 0;
}
Sdadaf
#include <iostream>
#include <map>
#include <queue>
using namespace std;
const int MAX = 1010;
int n;
struct HTreeNode {
    char data;
    int weight;
    int parent;
    int lchild;
    int rchild;
};
HTreeNode ht[MAX];
map<char, string> mp;
struct NodeType {
    int no;
    char data;
    int weight;
    bool operator < (const NodeType &s) {
        return s.weight < weight;
    }
};
void init() {
    int i;
    map<char, int> mp;
    for (int i = 0; i < n; i ++)
        mp[str[i]] ++;
    n = mp.size();
    i = 0;
    for (auto it : mp) {
        ht[i].data = it.first;
        ht[i].weight = it.second;
        i ++;
    }
    for (int j = 0; j < 2 * n - 1; j ++) {
        ht[j].lchild = ht[j].rchild = ht[j].parent = -1;
    }
}
void createHTree() {
    NodeType e, e1, e2;
    priority_qeue<NodeType> qu;
    for (int i = 0; i < n; i ++) {
        e.no = i;
        e.data = ht[i].data;
        e.weight = ht[i].weight;
        qu.push(e);
    }
    for (int j = n; j < 2 * n - 1; j ++) {
        e1 = qu.top(); qu.pop();
        e2 = qu.top(); qu.pop();
        ht[j].weight = e1.weight + e2.weight;
        ht[j].lchild = e1.no;
        ht[j].rchild = e2.no;
        ht[e2.no].parent = j;
        ht[e2.no].parent = j;
        e.no = j;
        e.weight = e1.weight + e2.weight;
        qu.push(e);
    }
}
void CreateHCode() {
    string code;
    code reserve(MAX);
}
int main() {
    cin >> n;
    return 0;
}


目录
相关文章
|
19天前
|
算法 数据安全/隐私保护
火山中文编程 -- MD5算法和SHA算法
火山中文编程 -- MD5算法和SHA算法
25 0
火山中文编程 -- MD5算法和SHA算法
|
19天前
|
算法 Java
算法编程(三十):交替合并字符串
算法编程(三十):交替合并字符串
51 0
|
19天前
|
算法
算法编程(二十八):重新排列单词间的空格
算法编程(二十八):重新排列单词间的空格
39 0
|
19天前
|
算法
算法编程(二十七):千位分隔数
算法编程(二十七):千位分隔数
45 0
算法编程(二十七):千位分隔数
|
19天前
|
存储 算法
算法编程(二十六):判断路径是否相交
算法编程(二十六):判断路径是否相交
32 0
|
19天前
|
算法
算法编程(二十五):检查单词是否为句中其他单词的前缀
算法编程(二十五):检查单词是否为句中其他单词的前缀
36 0
|
19天前
|
机器学习/深度学习 算法
机器学习 - [集成学习]Bagging算法的编程实现
机器学习 - [集成学习]Bagging算法的编程实现
45 1
|
19天前
|
机器学习/深度学习 算法 C语言
【编码狂想】深度探索C++编程之旅:“数组、字符串、函数与KMP算法解密“
【编码狂想】深度探索C++编程之旅:“数组、字符串、函数与KMP算法解密“
78 0
|
19天前
|
存储 算法 JavaScript
Java入门高频考查算法逻辑基础知识3-编程篇(超详细18题1.8万字参考编程实现)
解决这类问题时,建议采取下面的步骤: 理解数学原理:确保你懂得基本的数学公式和法则,这对于制定解决方案至关重要。 优化算法:了解时间复杂度和空间复杂度,并寻找优化的机会。特别注意避免不必要的重复计算。 代码实践:多编写实践代码,并确保你的代码是高效、清晰且稳健的。 错误检查和测试:要为你的代码编写测试案例,测试标准的、边缘情况以及异常输入。 进行复杂问题简化:面对复杂的问题时,先尝试简化问题,然后逐步分析和解决。 沟通和解释:在编写代码的时候清晰地沟通你的思路,不仅要写出正确的代码,还要能向面试官解释你的
38 0
|
19天前
|
存储 算法 JavaScript
【C++ 泛型编程 入门篇】 C++ 中的泛型算法 STL(sort,find)(二)
【C++ 泛型编程 入门篇】 C++ 中的泛型算法 STL(sort,find)
45 0