北理工计算机考研复试上机2015年真题

1、图形输出:输入 0-9 内的奇数,输出用*组成的正方形中间掏出来一个空的菱形。

范围错误则输出input error

例如:

输入:9

输出如下:

输入:5

输出:

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
	int n; 
	cin >> n;
	if (n%2==0 || n > 9 || n < 0) {
		cout << "input error" << endl;
		return 0;
	}
	vector<string> ans;
	// 输出第一行
	cout << string(n, '*') << endl;
	ans.push_back(string(n, '*')); 
	
	int h = n/2;
	for (int i = 0; i < h; i++) {
		string s;
		cout << string(h-i, '*');
		s += string(h-i, '*');
		cout << string(2*i+1, ' ');
		s += string(2*i+1, ' ');
		cout << string(h-i,'*');
		s += string(h-i,'*');
		ans.push_back(s);
		cout << endl;
	}
	for (int i = ans.size()-2; i>=0; i--) {
		cout << ans[i] << endl;
	}
	
}

2、有3个字母a,b,c:你输入一个数字,要输出所有的组合字符和组合数

输入: 1    输出: a,b,c 3

输入: 2    输出: aa,ab,ac,ba,bb,bc,ca,cb,cc 9

建议:这道题是一道全排列题目,力扣上有对应题目:力扣,46.全排列

同时,关于回溯模板,我整理了一篇题解,如有兴趣可以了解一下,回溯模板总结

代码:

#include <bits/stdc++.h>

using namespace std;
vector<vector<char>> ans; 
vector<char> cur; // 当前符合条件的结果
vector<char> nums = {'a','b','c'};  
vector<bool> st(nums.size(), false);
int n;
void dfs() {
    // 进入一层循环 首先判断cur是否满足条件
    if (cur.size() == n) {
        ans.push_back(cur);
        return;
    }
    for (int i = 0; i < nums.size(); i++) {
        // if (!st[i]) {
            cur.push_back(nums[i]);
            // st[i] = true;
            dfs();
            // st[i] = false;
            cur.pop_back();
        // }
    }
}

int main()
{
    cin >> n;
    dfs();
    for (auto it : ans) {
        for (auto it2 : it) cout << it2;
        cout << ' ';
    }
    cout << ans.size() << endl;
}

3、表达式展开,比如输入a-(b+c),输出a-b-c

输入: a-(b-(((c+d))))

输出: a-b+c+d 

思路提示:遍历过程中栈的最大深度即为答案

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    stack<pair<char,int>> st;
    string s;
    cin >> s;
    for (int i = s.size()-1; i >= 0; i--) {
        if (s[i] == ')') st.push({')', i});
        else if (s[i] == '+') st.push({'+', i});
        else if (s[i] == '-') st.push({'-', i});
        else if (s[i] == '(' && i > 1 && s[i-1] == '-') {
            int nl = 1;
            stack<pair<char,int>> cur; // 存储删除的元素
            while (nl > 0) {
                if (st.top().first == '+') {
                    st.top().first = '-';
                    cur.push(st.top());
                    st.pop();
                } else if (st.top().first == '-') {
                    st.top().first = '+';
                    cur.push(st.top());
                    st.pop();
                } else if (st.top().first == ')') {
                    nl--;
                    cur.push(st.top());
                    st.pop();
                } else if (st.top().first == '(') {
                    nl++;
                    cur.push(st.top());
                    st.pop();
                } 
                // cout << "*";
            }
            // 处理完 
            // 将cur里面的元素再加入到st中
            while (cur.size() > 0) {
                st.push(cur.top());
                cur.pop();
            }
            // cout << "---";
            st.push({s[i], i});  // 加入(  
        }   
    }
    // cout << "+++";
    while (st.size() > 0) {
        if (st.top().first == '-' || st.top().first == '+') {
            s[st.top().second] = st.top().first;
        }
        st.pop();
    }

    for (int i = 0; i < s.size(); i++) {
        if (s[i] != '(' && s[i] != ')') cout << s[i];
    }


    // a-(b+c-(e-(f+g)))   a-b-c+e-f-g
    // a-(b-c)
    // a-(b-(((c+d))))  a-b+c+d
    return 0;
}

4、求字符串1与字符串2的最大公共子串的长度及此长度最大公共子串的个数。

输入: abcdefg Eebcdfg  (最大公共子串:bcd)    

输出: 3 1

输入: abcdefg abcddefg  (最大公共子串为:abcd defg)

输出: 4 2

代码:

纯暴力版本:

#include <bits/stdc++.h>

using namespace std;
int main()
{
	string s1, s2;
	cin >> s1;
    cin >> s2;
	int maxlen = 0;
	int cnt = 0;
	for (int i = 0; i < s1.size(); i++) {
		for (int j = 0; j < s2.size(); j++) {
			int p1 = i, p2 = j;
			while (p1 < s1.size() && p2 < s2.size() && s1[p1] == s2[p2]) {
				p1++, p2++;
			}
			int len = p1-i;
			if (maxlen == len) cnt++;
			else if (maxlen < len) {
				cnt = 1;
				maxlen = len;
			}
		}
	}	
	cout << maxlen << ' ' << cnt << endl;
}

动态规划版:

#include <bits/stdc++.h>

using namespace std;
// 动态规划版本
int dp[1000][1000];
int main()
{
	memset(dp, 0, sizeof dp);
	string s1 = "0", s2 = "0", s;
	cin >> s;
	s1 += s;
	cin >> s;
	s2 += s;

	int maxlen = 0;
	int cnt = 0;
	for (int i = 1; i < s1.size(); i++) {
		for (int j = 1; j < s2.size(); j++) {
			if (s1[i] == s2[j])
				dp[i][j] = dp[i-1][j-1] + 1;
				if (dp[i][j] == maxlen) cnt++;
				else if (dp[i][j] > maxlen) {
					cnt = 1;
					maxlen = dp[i][j];
				}
				// maxlen = max(maxlen, dp[i][j]);
		}
	}
	
	cout << maxlen << ' ' << cnt << endl;
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值