首页 > 其他 > 详细

北京理工大学复试上机--2019

时间:2020-04-04 01:02:31      阅读:75      评论:0      收藏:0      [点我收藏+]
1、字符串解析将字符串看成不同的字符切片,切片不可重复,按字母序输出所有切片(每个切片一行) 
输入: aaabbcaaabaa  
输出: 
aa
aaa
b
bb
c
#include <iostream>
#include <set>
using namespace std;

int main() {
    string s;
    while (cin >> s) {
        set<string> ss;
        string str;
        int i;
        for (i = 0; i < s.length() - 1; i++) {
            str += s[i];
            if (s[i] != s[i + 1]) {
                ss.insert(str);
                str = "";
            }
        }
        str += s[i];
        ss.insert(str);
        for (auto it = ss.begin(); it != ss.end(); it++)
            cout << *it << endl;
    }
    return 0;
}

 

2、哈弗曼树求最小带权路径长度
输入:
4
1 1 1 1
输出: 8
 
输入:
4
22 5 6 3
输出: 76
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n, i, sum;
    vector<int> v;
    while (cin >> n) {
        int a[n];
        for (i = 0; i < n; i++) {
            cin >> a[i];
            v.push_back(a[i]);
        }
        sort(v.begin(), v.end());
        sum = 0;
        while (v.size() != 1) {
            sum += (v[0] + v[1]);
            v.push_back(v[0] + v[1]);
            v.erase(v.begin());
            v.erase(v.begin());
            sort(v.begin(), v.end());
        }
        cout << sum << endl;
    }
    return 0;
}

 

 

北京理工大学复试上机--2019

原文:https://www.cnblogs.com/ache/p/12630066.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!