程序设计期末复习

Posted 超霸霸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了程序设计期末复习相关的知识,希望对你有一定的参考价值。

程序设计期末复习

7891 一元三次方程求解

#include<iostream>
#include<iomanip>
using namespace std;

double a, b, c, d;

double f(double x) 
	return a * x * x * x + b * x * x + c * x + d;


int main() 
	cin >> a >> b >> c >> d;
	double x1, x2, mid;
	for (int i = -100; i < 100; i++) 
		x1 = i;
		x2 = i + 1;
		if (f(x1) == 0) 
			cout << fixed << setprecision(2) << x1 << " ";
		
		else if (f(x1) * f(x2) < 0) 
			while (x2 - x1 >= 0.001) 
				mid = (x1 + x2) / 2;
				if (f(x1) * f(mid) <= 0) 
					x2 = mid;
				
				else 
					x1 = mid;
				
			
			cout << fixed << setprecision(2) << x1 << " ";
		
	
	return 0;

7617 分治法输出前k大的数

#include<iostream>
#include<algorithm>
using namespace std;

int a[100000], n, k;

bool cmp(int a, int b) 
	return a > b;


int main()

	cin >> n;
	for (int i = 0; i < n; i++) 
		cin >> a[i];
	
	cin >> k;
	sort(a, a + n, cmp);
	for (int i = 0; i < k; i++) 
		cout << a[i] << endl;
	
	return 0;

7620 分治法区间合并

#include<iostream>
#include<algorithm>

using namespace std;

int n, head, tail;

struct node 
	int start;
	int end;
a[60000];

bool cmp(node a, node b) 
	if (a.start == b.start) 
		return a.end < b.end;
	
	return a.start < b.start;


int main() 
	cin >> n;
	for (int i = 0; i < n; i++) 
		cin >> a[i].start >> a[i].end;
	
	sort(a, a + n, cmp);
	head = a[0].start;
	tail = a[0].end;
	for (int i = 1; i < n; i++) 
		if (a[i].start > tail) 
			cout << "no" << endl;
			return 0;
		
		tail = max(tail, a[i].end);
	
	cout << head << " " << tail << endl;
	return 0;

1775 采药

#include<iostream>

using namespace std;

int N, W, w[2000], v[2000], dp[2000][2000];

int main() 
	cin >> W >> N;
	for (int i = 1; i <= N; i++) 
		cin >> w[i] >> v[i];
	
	for (int i = 1; i <= N; i++) 
		for (int j = 0; j <= W; j++) 
			if (j < w[i]) 
				dp[i][j] = dp[i - 1][j];
			
			else 
				dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
			
		
	
	cout << dp[N][W];
	return 0;

4982 踩方格

#include<iostream>
using namespace std;

int dp[30];

int main() 
	int n;
	cin >> n;
	dp[0] = 1;
	dp[1] = 3;
	for (int i = 2; i <= n; i++) 
		dp[i] = dp[i - 2] + 2 * dp[i - 1];
	
	cout << dp[n];
	return 0;

2718 移动路线

#include<iostream>

using namespace std;

int m, n, dp[25][25];

int main() 
	cin >> m >> n;
	dp[1][1] = 1;
	for (int i = 1; i <= m; i++) 
		for (int j = 1; j <= n; j++) 
			dp[i][j] += dp[i - 1][j];
			dp[i][j] += dp[i][j - 1];
		
	
	cout << dp[m][n];
	return 0;

6252 带通配符的字符串匹配

#include<iostream>

using namespace std;

string s1, s2;
int dp[25][25]; //dp[i][j]为s1到i,s2到j是否匹配

int main() 
	cin >> s1 >> s2;
	s1 = ' ' + s1;
	s2 = ' ' + s2;
	dp[0][0] = 1;
	// s开头全为*的情况
	for (int i = 1; i < s1.size(); i++) 
		if (s1[i] == '*') 
			dp[i][0] = 1;
		
		else 
			break;
		
	
	for (int i = 1; i < s1.size(); i++) 
		for (int j = 1; j < s2.size(); j++) 
			if (s1[i] == '?') 
				dp[i][j] = dp[i - 1][j - 1];
			
			else if (s1[i] == '*') 
				dp[i][j] = (dp[i - 1][j - 1] || dp[i][j - 1] || dp[i - 1][j]);
			
			else if (s1[i] == s2[j]) 
				dp[i][j] = dp[i - 1][j - 1];
			
			else if (s1[i] != s2[j]) 
				dp[i][j] = 0;
			
		
	
	if (dp[s1.size() - 1][s2.size() - 1] == 1) 
		cout << "matched";
	
	else 
		cout << "not matched";
	
	return 0;

702 crossing river

#include<iostream>
#include<algorithm>

using namespace std;

int n, t, dp[2000];

void init() 
	cin >> n;
	for (int i = 1; i <= n; i++) 
		cin >> dp[i];
	
	sort(dp + 1, dp + n + 1);


void solve() 
	int res = 0;
	while (n) 
		if (n == 1) 
			res += dp[1];
			break;
		
		else if (n == 2) 
			res += dp[2];
			break;
		
		else if (n == 3) 
			res += dp[1] + dp[2] + dp[3];
			break;
		
		else if (n >= 4) 
			// 方案1: 最快和次快渡河,最快返回,最慢和次慢渡河,次快返回
			// 方案2: 最快和最慢渡河,最快返回,最快和次慢渡河,最快返回
			res += min(2 * dp[2] + dp[1] + dp[n], 2 * dp[1] + dp[n] + dp[n - 1]);
			n -= 2;
		
	
	cout << res << endl;


int main() 
	cin >> t;
	while (t--) 
		init();
		solve();
	
	return 0;

1799 最短前缀

#include<iostream>
#include<cstring>

using namespace std;

char str[2000][50], pre[50], temp[50];
int i, j, k;

int main() 
	int n = 0;
	//循环输入str并获得字符串个数
	while (scanf("%s", str[n]) != EOF) 
		n++;
	

	//循环遍历字符串数组
	for (i = 0; i < n; i++) 
		//遍历该字符串str[i]
		for (j = 1; j < strlen(str[i]); j++) 
			strcpy(pre, str[i]);
			pre[j] = '\\0';
			//遍历字符串数组,使前缀不为其他字符串的子串
			for (k = 0; k < n; k++) 
				if (k == i) 
					continue;
				
				strcpy(temp, str[k]);
				temp[j] = '\\0';
				if (strcmp(temp, pre) == 0) 
					break;
				
			
			if (k == n) 
				break;
			
		
		if (j < strlen(str[i])) 
			cout << str[i] << " " << pre << endl;
		
		else 
			cout << str[i] << " " << str[i] << endl;
		
	

	return 0;

以上是关于程序设计期末复习的主要内容,如果未能解决你的问题,请参考以下文章

程序设计期末复习

程序设计期末复习

山东科技大学 软件安全 期末复习

期末复习——同步互斥死锁

期末复习考试月来临!☀️C语言复习,这一篇带你逃离挂科区!(上)⭐️

期末复习考试月来临!☀️C语言复习,这一篇带你逃离挂科区!(上)⭐️