• 郑州大学2022-2023第一学期算法设计与分析-实验3


    1 插座问题

    #include
    using namespace std;
    
    signed main() 
    {
    	int n, k; cin >> n >> k;
    	int t = (n - 1) / (k - 1);
    	cout << (t * (k - 1) == n - 1 ? t : t + 1) << '\n';
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2 办事大厅排队

    #include
    #include
    #include
    using namespace std;
    
    signed main() 
    {
    	int T; cin >> T;
    	queue<string> q;
    	string op, name;
    	while(T --)
    	{
    		cin >> op;
    		if(op[0] == 'i') cin >> name, q.push(name);
    		else if(op[0] == 'o') q.pop();
    		else if(op[0] == 'q')
    		{
    			if(q.empty()) cout << "NULL" << '\n';
    			else cout << q.front() << '\n';
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3 字符串的全排列

    #include
    #include
    #include
    using namespace std;
    
    signed main() 
    {
    	string s; cin >> s;
    	sort(s.begin(), s.end());
    	
    	cout << s << '\n';
    	while(next_permutation(s.begin(), s.end())) 
    		cout << s << '\n';
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    4 集合的“交”与“并”

    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    signed main() 
    {
    	int n, m, x; cin >> n >> m;
    	unordered_set<int> s1, s2;
    	vector<int> vec1, vec2;
    	
    	for(int i = 1; i <= n; i ++)
    		cin >> x, s1.insert(x);
    	for(int i = 1; i <= m; i ++)
    		cin >> x, s2.insert(x);
    	
    	for(auto x : s1)
    		if(s2.count(x)) vec1.push_back(x);
    		else vec2.push_back(x);
    	
    	for(auto x : s2) vec2.push_back(x);
    	
    	sort(vec1.begin(),vec1.end());
    	sort(vec2.begin(),vec2.end());
    	
    	cout << vec1.size();
    	for(auto x : vec1) cout << " " << x;
    	cout << '\n';
    	cout << vec2.size();
    	for(auto x : vec2) cout << " " << x;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    5 统计英文单词个数

    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    void solve()
    {
    	map<string,int> mp;
    	int n; cin >> n;
    	string s;
    	while(n --) cin >> s, mp[s] ++;
    	for(auto [x, c] : mp) cout << x << " " << c << '\n';
    }
    
    signed main() 
    {
    	int T; cin >> T;
    	while(T --) solve();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    6 括号匹配

    #include 
    #include 
    #include 
    #include 
    using namespace std;
    void solve()
    {
        string s; cin >> s;
    	stack<char> stk;
    	for(int i = 0; i < s.size(); i ++)
    	{
    		if(s[i] == '(' || s[i] == '[' || s[i] == '{') stk.push(s[i]);
    		else
    		{
    			if(stk.empty())  stk.push('#');
    			if(stk.top() == '(' && s[i] == ')' || stk.top() == '[' && s[i] == ']' || stk.top() == '{' && s[i] == '}') 
    			    stk.pop();
    			else stk.push('#');
    		}
    	}
    	if(stk.empty()) cout << "Yes" << '\n';
    	else cout << "No" << '\n';
    }
    
    signed main() 
    {
    	int T; cin >> T;
    	while(T --) solve();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    7 后续+中序序列构造二叉树

    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    string post, in, pre = "";
    unordered_map<char,int>pos;
    void dfs(int il, int ir, int pl, int pr)
    {
    	if(pl > pr) return ;
    	int k = pos[post[pr]];
    	pre += post[pr];
    	dfs(il, k - 1, pl, pl + k - 1 - il);
    	dfs(k + 1, ir, pl + k - 1 - il + 1, pr - 1);
    }
    signed main() 
    {
    	int n; cin >> n;
    	cin >> post >> in; 
    	for(int i = 0; i < n; i ++) pos[in[i]] = i;
    	dfs(0, n - 1, 0, n - 1);
    	cout << pre << '\n';
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    8 找第 k 小的数

    #include 
    using namespace std;
    const int N = 100010;
    int a[N];
    int find(int l, int r, int k)
    {
        if(l >= r) return a[l];
        
        int i = l - 1, j = r + 1, v = a[l + r >> 1];
        while(i < j)
        {
            do i ++; while(a[i] < v);
            do j --; while(a[j] > v);
            if(i < j) swap(a[i], a[j]);
        }
        
        if(j - l + 1 >= k) return find(l, j, k);
        else return find(j + 1, r, k - (j - l + 1));
    }
    
    signed main()
    {
        int n, k; cin >> n >> k;
        for(int i = 0; i < n; i ++) cin >> a[i];
        cout << find(0, n - 1, k) << '\n';
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    9 求逆序对数目

    #include 
    using namespace std;
    #define int long long
    const int N = 100010;
    int a[N], tmp[N];
    int res = 0;
    void merge(int l, int r)
    {
    	if(l >= r) return ;
    	int mid = l + r >> 1;
    	merge(l, mid);
    	merge(mid + 1, r);
    	int i = l, j = mid + 1, k = 0;
    	while(i <= mid && j <= r)
    	{
    		if(a[i] <= a[j]) tmp[k ++] = a[i ++];
    		else
    		{
    			res += mid - i + 1;
    			tmp[k ++] = a[j ++];
    		}
    	}
    	while(i <= mid) tmp[k ++] = a[i ++];
    	while(j <= r) tmp[k ++] = a[j ++];
    	for(int i = l, k = 0; i <= r; i ++, k ++) a[i] = tmp[k];
    }
    signed main()
    {
        int n; cin >> n;
        for(int i = 0; i < n; i ++) cin >> a[i];
        merge(0, n - 1);
        cout << res << '\n';
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
  • 相关阅读:
    Windows与网络基础-15-本地安全策略
    Google Play 开发者账户被封 如何改代码快速上架
    【Java基础】成员变量和局部变量及封装
    Python 安装js环境
    L70.linux命令每日一练 -- 第十章 Linux网络管理命令 -- nc和ssh
    mysql5.7.35安装配置教程【超级详细安装教程】
    通信原理学习笔记1:模拟与数字通信系统、通信系统性能指标
    HBase基本操作及命令示例
    前端面试题目(二十九)
    Anchor-Based:Anchor-Free你很会打吗?Waymo实时目标检测新SOTA!
  • 原文地址:https://blog.csdn.net/qq_51282224/article/details/127573492