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

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

7-17 大数和


#include <iostream>
#include <vector>
using namespace std;
vector<int> add(vector<int> &A, vector<int> &B) {
    vector<int> C;
    for (int i = 0, t = 0; i < A.size() || i < B.size() || t; i ++) {
        if (i < A.size()) t += A[i];
        if (i < B.size()) t += B[i];
        C.push_back(t % 10);
        t /= 10;
    }
    while (C.back() == 0 && C.size() > 1)
        C.pop_back();
    return C;
}
vector<int> sub(vector<int> &A, vector<int> &B) {
    vector<int> C;
    for (int i = 0, t = 0; i < A.size(); i ++) {
        t = A[i] - t;
        if (i < B.size()) t -= B[i];
        C.push_back((t + 10) % 10);
        if (t < 0) t = 1;
        else t = 0;
    }
    while (C.back() == 0 && C.size() > 1)
        C.pop_back();
    return C;
}
bool cmp(vector<int> &A, vector<int> &B) {
    if (A.size() != B.size())
        return A.size() > B.size();
    for (int i = 0; i < A.size(); i ++)
        if (A[i] != B[i]) return A[i] > B[i];
    return true;
}
int n;
int main() {
    while (cin >> n, n) {
        int a1 = 1, ans1 = 1;
        vector<int> ans = {0};
        while (n --) {
            string a;
            vector<int> A;
            cin >> a;
            if (a[0] == '-') {
                a1 = 0;
                for (int i = a.size() - 1; i > 0; i --)
                    A.push_back(a[i] - '0');
            } else {
                for (int i = a.size() - 1; i >= 0; i --)
                    A.push_back(a[i] - '0');
                a1 = 1;
            }
            if (cmp(ans, A)) {
                if (ans1 && a1) ans = add(ans, A), ans1 = 1;
                else if (ans1 && !a1) ans = sub(ans, A), ans1 = 1;
                else if (!ans1 && !a1) ans = add(ans, A), ans1 = 0;
                else ans = sub(ans, A), ans1 = 0;
            } else {
                if (ans1 && a1) ans = add(ans, A), ans1 = 1;
                else if (ans1 && !a1) ans = sub(A, ans), ans1 = 0;
                else if (!ans1 && !a1) ans = add(ans, A), ans1 = 0;
                else ans = sub(A, ans), ans1 = 1;
            }
        }
        if (!ans1 && ans[0]) cout << '-';
        for (int i = ans.size() - 1; i >= 0; i --)
            cout << ans[i];
        cout << endl;
    }
    return 0;
}


7-18 求最大公约数


#include <iostream>
using namespace std;
int f;
int a, b;
int gcd(int a, int b) {
    if (a % b == 0) return b;
    else {
//         if (f) cout << ' ';
        printf(" gcd(%d,%d)", b, a % b);
        return gcd(b, a % b);
    }
}
int main() {
    cin >> a >> b;
    printf("gcd(%d,%d)", a, b);
    int res = gcd(a, b);
    cout << ' ' << res;
    return 0;
}


7-19 二分查找


#include <iostream>
using namespace std;
const int N = 1010;
int n, arr[N], x, cnt;
int main() {
    cin >> n;
    for (int i = 0; i < n; i ++)
        cin >> arr[i];
    cin >> x;
    int l = 0, r = n - 1;
    while (l <= r) {
        int mid = (l + r) >> 1;
        if (arr[mid] > x) r = mid - 1,cnt ++;
        else if (arr[mid] < x) l = mid + 1, cnt ++;
        else {
            cnt ++;
            cout << mid << endl << cnt << endl;
            return 0;
        }
    }
    cout << -1 << endl << cnt << endl;
    return 0;
}


7-20 全排列(分治)


#include <iostream>
using namespace std;
const int N = 1010;
int path[N], n;
void dfs(int u) {
    if (u == n) {
        for (int i = 1; i <= n; i ++)
            cout << path[i] << ' ';
        cout <<endl;
        return ;
    }
    for (int i = u; i <= n; i ++) {
        swap(path[i], path[u]);
        dfs(u + 1);
        swap(path[u], path[i]);
    }
}
int main() {
    cin >> n;
    for (int i = 1; i <= n; i ++)
        path[i] = i;
    dfs(1);
    return 0;
}


7-21 我是送分题


#include <iostream>
#include <cstring>
using namespace std;
const int mod = 1e9 + 7;
typedef long long LL;
// 结论需要推
struct Mx {
    LL g[3][3];
    Mx() {
        memset(g, 0, sizeof g);
    }
};
// 矩阵乘法 3 重循环
Mx mul(Mx a, Mx b) {
    Mx res;
    for (int i = 0; i < 3; i ++)
        for (int j = 0; j < 3; j ++) {
            LL sum = 0;
            for (int k = 0; k < 3; k ++)
                sum = (sum + a.g[i][k] * b.g[k][j]) % mod;
            res.g[i][j] = sum;
        }
    return res;
}
// 矩阵快速幂
Mx Mx_pow(Mx a, LL b) {
    Mx res;
    // 构造单位矩阵
    res.g[0][0] = res.g[1][1] = res.g[2][2] = 1;
    while (b) {
        if (b & 1) res = mul(res, a); // b & 1 判断 b 的末尾是否为 0
        a = mul(a, a);
        b >>= 1;
    }
    return res;
}
LL n;
Mx a;
int main() {
    cin >> n;
    // 目标矩阵
    a.g[0][0] = a.g[1][0] = a.g[2][1] = 1;
    a.g[0][1] = 2;
    a.g[0][2] = 3;
    a = Mx_pow(a, n - 3);
    LL ans[3][1], num[3][1];
    num[0][0] = 3, num[1][0] = 2, num[2][0] = 1;
    for (int i = 0; i < 3; i ++) {
        LL sum = 0;
        for (int j = 0; j < 3; j ++)
            sum = (sum + a.g[i][j] * num[j][0]) % mod;
        ans[i][0] = sum;
    }
    cout << ans[0][0] << endl;
    return 0;
}


7-22 二分查找


#include <iostream>
#include <cstring>
using namespace std;
const int N = 1010;
int n, x, t;
int main() {
    scanf("%d%d", &n, &x);
    for (int i = 0; i < n; i ++) {
        cin >> t;
        if (t >= x) {
            cout << i + 1;
            return 0;
        }
    }
    cout << n + 1 << endl;
    return 0;
}


7-23 程序设计综合实践 2.1


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
    vector<int>ans;
    int x;
    while (cin >> x) ans.push_back(x);
    sort(ans.begin(), ans.end());
    cout << ans[0] << ',' << ans[ans.size() - 1];
    return 0;
}


7-24 找第k小的数


#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    int n, m;
    cin >> n >> m;
    int a[n];
    for (int i = 0; i < n; i ++)
        cin >> a[i];
    sort(a, a + n);
    cout << a[m - 1] << endl;
    return 0;
}


7-25 第 k 大的整数**


#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    int n, m;
    cin >> n >> m;
    int a[n];
    for (int i = 0; i < n; i ++)
        scanf("%d", &a[i]);
    sort(a, a + n);
    cout << a[n - m] << endl;
    return 0;
}


7-26 0/1背包问题


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


7-27 子集和问题


#include <iostream>
using namespace std;
const int N = 1010;
int n, m;
int idx, sum;
int w[N], op[N];
void dfs(int idx, int sum) {
    if (idx > n || sum > m) return ;
    else if (sum == m) {
        for (int i = 0; i < n; i ++)
            if (op[i])
                cout << w[i] << ' ';
        cout << endl;
        return ;
    } else {
        op[idx] = 1;
        dfs(idx + 1, sum + w[idx]);
        op[idx] = 0;
        dfs(idx + 1, sum);
    }
}
int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i ++)
        cin >> w[i];
    dfs(0, 0);
    return 0;
}


7-28 幂集(回溯法)


#include <iostream>
using namespace std;
const int N = 1010;
int n, arr[N], cnt;
int st[N];
void dfs(int a[], int n, int i, int st[]) {
    if (i >= n) cnt ++;
    else {
        st[i] = 1;
        dfs(a, n, i + 1, st);
        st[i] = 0;
        dfs(a, n, i + 1, st);
    }
}
int main() {
    cin >> n;
    for (int i = 0; i < n; i ++)
        cin >> arr[i];
    dfs(arr, n, 0, st);
    cout << cnt << endl;
    return 0;
}


7-29 看电影


#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[1010];
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() {
    while (cin >> n, n != 0) {
        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-30 CPA招新Ⅰ


#include <iostream>
using namespace std;
int main() {
    char e;
    int a = 0, b = 0, c = 0, d = 0;
    while ((e = getchar()) != '\n') {
        if (e == 'A') a ++;
        else if(e == 'B') b ++;
        else if (e == 'C') c ++;
        else if (e == 'D') d ++;
    }
    printf("Competition department %d people!\n", a);
    printf("Propaganda Department %d people!\n", b);
    printf("Office %d people!\n", c);
    printf("Organization Department %d people!\n", d);
    return 0;
}


7-31 多参加活动,生活才精彩


#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, score = 0;
    for (int i = 0; i < n; i ++) {
        cin >> a[i].b >> a[i].e >> a[i].no;
//         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 (a[i].b >= pre) {
            pre = a[i].e;
//             cout << a[i].b << ' ' << a[i].e << endl;
            cnt ++;
            score += a[i].no;
        }
    }
    cout << cnt << ' ' << score << endl;
    return 0;
}


7-32 装箱问题


#include <iostream>
using namespace std;
const int N = 1010;
int n, max_x = -1;
int a[N], b[N];
int main() {
    cin >> n;
    for (int i = 1; i<= n; i ++)
        cin >> a[i], b[i] = 100;
    for (int i = 1; i <= n; i ++) {
        for (int j = 1; j <= n; j ++) {
            if (a[i] <= b[j]) {
                b[j] -= a[i];
                cout << a[i] << ' ' << j << endl;
                max_x = max(max_x, j);
                    break;
            }
        }
    }
    cout << max_x << endl;
    return 0;
}
目录
相关文章
|
3月前
|
算法 数据安全/隐私保护
火山中文编程 -- MD5算法和SHA算法
火山中文编程 -- MD5算法和SHA算法
36 0
火山中文编程 -- MD5算法和SHA算法
|
3月前
|
机器学习/深度学习 算法
机器学习 - [集成学习]Bagging算法的编程实现
机器学习 - [集成学习]Bagging算法的编程实现
59 1
|
7天前
|
存储 算法
【C算法】编程初学者入门训练140道(1~20)
【C算法】编程初学者入门训练140道(1~20)
|
3月前
|
存储 分布式计算 算法
【底层服务/编程功底系列】「大数据算法体系」带你深入分析MapReduce算法 — Shuffle的执行过程
【底层服务/编程功底系列】「大数据算法体系」带你深入分析MapReduce算法 — Shuffle的执行过程
63 0
|
29天前
|
存储 算法 搜索推荐
告别低效编程!Python算法设计与分析中,时间复杂度与空间复杂度的智慧抉择!
【7月更文挑战第22天】在编程中,时间复杂度和空间复杂度是评估算法效率的关键。时间复杂度衡量执行时间随数据量增加的趋势,空间复杂度关注算法所需的内存。在实际应用中,开发者需权衡两者,根据场景选择合适算法,如快速排序(平均O(n log n),最坏O(n^2),空间复杂度O(log n)至O(n))适合大规模数据,而归并排序(稳定O(n log n),空间复杂度O(n))在内存受限或稳定性要求高时更有利。通过优化,如改进基准选择或减少复制,可平衡这两者。理解并智慧地选择算法是提升代码效率的关键。
28 1
|
1月前
|
存储 算法 Python
震撼!Python算法设计与分析,分治法、贪心、动态规划...这些经典算法如何改变你的编程世界!
【7月更文挑战第9天】在Python的算法天地,分治、贪心、动态规划三巨头揭示了解题的智慧。分治如归并排序,将大问题拆解为小部分解决;贪心算法以局部最优求全局,如Prim的最小生成树;动态规划通过存储子问题解避免重复计算,如斐波那契数列。掌握这些,将重塑你的编程思维,点亮技术之路。
39 1
|
2月前
|
机器学习/深度学习 算法 搜索推荐
编程之舞:探索算法的优雅与力量
【6月更文挑战第10天】在软件的世界里,算法是构筑数字宇宙的基石。它们如同精心编排的舞蹈,每一个步骤都充满着逻辑的美感和解决问题的力量。本文将带领读者走进算法的世界,一起感受那些精妙绝伦的编程思想如何转化为解决现实问题的钥匙。
21 3
|
2月前
|
人工智能 算法 搜索推荐
Java算法编程详解和程序实例
Java算法编程详解和程序实例
28 0
|
3月前
|
机器学习/深度学习 算法 C语言
【编码狂想】深度探索C++编程之旅:“数组、字符串、函数与KMP算法解密“
【编码狂想】深度探索C++编程之旅:“数组、字符串、函数与KMP算法解密“
90 0
|
2月前
|
算法 安全 数据挖掘
解锁编程之门:数论在算法与加密中的实用应用
解锁编程之门:数论在算法与加密中的实用应用

热门文章

最新文章