2019中科大计算机考研复试上机题代码参考

2019-1

#include<iostream>
#include<vector>
#include<algorithm>
#include <string>
using namespace std;
int w[17] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
char m[11] = { '1','0','X','9','8','7','6','5','4','3','2' };

int main()
{
	string s;
	cin >> s;
	int sum = 0;
	for (int i = 0; i < 17; i++)
		sum += (s[i] - '0') * w[i];
	sum = sum % 11;
	cout << sum << endl;
	if (m[sum] == s[17])
		cout << "YES";
	else
		cout << "NO";

}

2019-2

#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
int low_bit(int x)
{
	return x& (-x);
}

int main()
{
	int n;
	cin >> n;
	int temp = n;
	int res = 0;
	while (temp)
	{
		temp -= low_bit(temp);
		res++;
	}
	vector<int>ans;
	if (res != 2)
		printf("NO");
	else
	{
		for (int i = 0; i < 32; i++)
		{
			if ((n >> i) & 1)
				ans.push_back(i);

		}
		printf("%d = 2^%d + 2^%d", n, ans[0], ans[1]);
	}
	
}

2019-3

#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
#include<fstream>
#include <string>
using namespace std;
stack<int>num;
void cal( char c)
{
	auto a = num.top();
	num.pop();
	auto b = num.top();
	num.pop();
	if (c == '+') num.push(a + b);
	else if (c == '-') num.push(a - b);
	else if (c == '*') num.push(a * b);
	else if (c == '/') num.push(a / b);
}
int main()
{
	char s;
	vector<char>str;
	ifstream ifs("pre.in");
	while (ifs >> s)
		str.push_back(s);
	for (int i = str.size() - 1; i >= 0; i--)
	{
		
		if (isdigit(str[i]))
		{
			num.push(str[i] - '0');
			continue;
		}
		cal(str[i]);
		
	}
	cout << num.top();
}

2019-4

我好菜,没想出来
#include<iostream>
#include<algorithm>
#include<vector>
#include<fstream>
using namespace std;
vector<vector<char>> subsets;
vector<char> str;

void print(vector<vector<char>>& subsets) {
	cout << '{';
	int flag1 = 0;
	for (auto& a : subsets) {
		if (flag1) cout << ',';
		cout << '{';
		int flag2 = 0;
		for (auto& b : a) {
			if (flag2) cout << ',';
			cout << b;
			flag2 = 1;
		}
		cout << '}';
		flag1 = 1;
	}
	cout << '}';
	cout << endl;
}

void dfs(int idx, vector<vector<char>>& subsets)
{
	if (idx == str.size())
	{
		print(subsets);
		return;
	}
	for (auto subset : subsets)
	{
		subset.push_back(str[idx]);
		dfs(idx + 1, subsets);
		subset.pop_back();
	}
	subsets.push_back({ str[idx] });
	dfs(idx + 1, subsets);
	subsets.pop_back();
}
int main()
{
	ifstream ifs("set.in");
	char s;
	while (ifs >> s)
		str.push_back(s);
	dfs(0,subsets);
	
}
引用自: (https://zdszero.github.io/posts/ustc-test-2019)

2019-5

类似leetcode 124
#include <iostream>
#include <fstream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>nums;
int ans;
int dfs(int u)
{
	if (u >= nums.size())
		return 0;
	int left = dfs(2*u + 1);//左子树的最值
	int right = dfs(2*u + 2);//右子树的最值
	ans = max(ans, nums[u] + left + right);
	return nums[u] + max(left, right);//只能返回一边,否则不能构成一个路径


}
int main()
{
	ifstream ifs("expr.in");
	int x;
	while (ifs >> x)
	{
		nums.push_back(x);
	}
	ans = INT_MIN;
	dfs(0);
	cout << ans;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值