郑州大学算法设计与分析实验2

发布时间:2024年01月05日

判断题
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1

#include<bits/stdc++.h> 

using namespace std;

const int N = 50;
int f[N], n;

int main()
{
//	freopen("1.in", "r", stdin);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	f[1] = 1; f[2] = 1;
	for(int i = 3; i <= n; ++ i)
		f[i] = f[i - 1]  + f[i - 2];
	cout << f[n];
}

2

#include<bits/stdc++.h> 

using namespace std;

const int N = 10000010;
int f[N], n;

int main()
{
//	freopen("1.in", "r", stdin);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	f[1] = 1; f[2] = 1;
	for(int i = 3; i <= n; ++ i)
		f[i] = (f[i - 1]  + f[i - 2]) % 998244353;
	cout << f[n];
}

3

#include<bits/stdc++.h> 
#define LL long long
using namespace std;

const LL mod = 998244353;
LL n;
LL f[3] = {0, 1, 1};
LL a[3][3] = {{0, 0, 0}, {1, 0, 1}, {0, 1, 1}};
void mulself(LL a[3][3])
{
	LL c[3][3] = {0};
	for(int i = 0; i < 3; ++ i)
		for(int j = 0; j < 3; ++ j)
			for(int k = 0; k < 3; ++ k)
				c[i][j] = (c[i][j] + (LL) a[i][k] * a[k][j]) % mod;
	memcpy(a, c, sizeof c);
}

void mul(LL f[3], LL a[3][3])
{
	LL c[3] = {0};
	for(int i = 0; i < 3; ++ i)
		for(int j = 0; j < 3; ++ j)
			c[i] = (c[i] + (LL) f[j] * a[j][i]) % mod;
	memcpy(f, c, sizeof c);		
}

int main()
{
	// freopen("2.in", "r", stdin);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;

	while(n)
	{
		if(n & 1) mul(f, a);
		mulself(a);
		n /= 2;
	}
	cout << f[0] << endl;
}

4

#include <bits/stdc++.h> 
#define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(register int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f 
#define x first
#define y second
using namespace std;

const int N = 1e4 + 10;
char a[N][N];
int n;

void solve(int n, int x, int y)
{
	if(n == 1)
	{
		a[x][y] = 'X';
		return;
	}
	int m = pow(3, n - 2);
	solve(n - 1, x, y);
	solve(n - 1, x, y + 2 * m);
	solve(n - 1, x + m, y + m);
	solve(n - 1, x + 2 * m, y);
	solve(n - 1, x + 2 * m, y + 2 * m);
}

int main()
{
//	freopen("2.in", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    while(cin >> n)
    {
    	if(n == -1)	break;
    	int q = pow(3, n - 1);
    	for(int i = 0; i < q; ++ i)
    	{
    		for(int j = 0; j < q; ++ j)
    			a[i][j] = ' ';
    		a[i][q] = '\0';
		}
    	solve(n, 0, 0);
    	for(int i = 0; i < q; ++ i)
    		cout << a[i] << endl;
        
    	cout << '-' << endl;
	}

	return 0; 	
}


5

#include<bits/stdc++.h> 
#define LL long long
using namespace std;

const int N = 100010;
int n, L;
double a[N], b[N], s[N];

bool check(double mid)
{
	for(int i = 1; i <= n; ++ i)
	{
		b[i] = a[i] - mid;
		s[i] = s[i - 1] + b[i];
	}
	
	double minn = 1e9;
	for(int i = L; i <= n; ++ i)
	{
		minn = min(minn, s[i - L]);
		if(s[i] - minn >= 0)	return true;
	}
	return false;
}

void solve()
{
	cin >> n >> L;
	for(int i = 1; i <= n; ++ i)	cin >> a[i];
	double l = 0, r = 1e9;
	while(r - l >= 1e-5)
	{
		double mid = (l + r) / 2;
		if(check(mid))	l = mid;
		else r = mid;	
	} 
	cout << (int)(r * 1000);
}

int main()
{
//	freopen("2.in", "r", stdin);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	solve();
}

6

#include<bits/stdc++.h> 
#define LL long long
using namespace std;

int n, k;
map<int, int>cnt;
void solve()
{
	cin >> n;
	for(int i = 1; i <= n; ++ i)
	{
		int id;	cin >> id;
		cnt[id] ++;
	}
	cin >> k;
	while(k --)
	{
		int id; cin >> id;
		if(cnt.find(id) == cnt.end())	puts("No");
		else puts("Yes");
	}
}

int main()
{
//	freopen("2.in", "r", stdin);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	solve();
}

7

#include <bits/stdc++.h> 
#define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(register int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f 
#define x first
#define y second
using namespace std;
const int N = 1e4 + 10;
LL n, x[N], y[N], ans;
int main()
{
//	freopen("2.in", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
	for(int i = 0; i < n; ++ i)	cin >> x[i] >> y[i];
	sort(x, x + n);
	sort(y, y + n);
	for(int i = 0; i < n; ++ i)
		x[i] -= (i + 1);
    sort(x, x + n);
	int mid_x = x[n / 2], mid_y = y[n / 2];
	for(int i = 0; i < n; ++ i)
		ans += abs(x[i] - mid_x),
		ans += abs(y[i] - mid_y);	
	cout << ans << endl;
	return 0; 	
}


8

#include <bits/stdc++.h> 
#define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(register int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f 
#define x first
#define y second
using namespace std;
const int N = 1e4 + 10;
int n, x, a[N];
int main()
{
    IOS
    cin>>n;
    for(int i = 0; i < n; i ++)
        cin >> x >> a[i];
    sort(a,a+n);
    int min=0;
    for(int i=0; i<n; i++)
        min += (int)fabs(a[i]-a[n/2]);
    cout<<min<<endl;
	return 0;
}


9

#include <bits/stdc++.h>
#define LL long long
 
using namespace std;
const int N = 1e6 + 10, INF = 1 << 30;
struct wy
{
	double x, y;
}p[N];
int n, tmp[N], pos1, pos2;
double ass;

double dis(wy a, wy b)
{
	double x =  (a.x - b.x) * (a.x - b.x);
	double y =  (a.y - b.y) * (a.y - b.y);
	return sqrt(x + y);
}

bool cmp1(wy a, wy b)
{
	if(a.x == b.x)	return a.y < b.y;
	return a.x < b.x; 
}

bool cmp2(int a, int b)
{
	return p[a].y < p[b].y;
}

double solve(int l, int r)
{
	if(l == r)	return INF;
	int mid = (l + r) >> 1;
	double d = INF;
	d = min(solve(l, mid), solve(mid + 1, r));
	int k = 0;
	for(int i = l; i <= r; ++ i)
		if(fabs(p[mid].x - p[i].x) < d)
			tmp[++ k] = i;
	sort(tmp + 1, tmp + 1 + k, cmp2);
	for(int i = 1; i <= k; ++ i)
		for(int j = i + 1; j <= k && p[tmp[j]].y - p[tmp[i]].y < d; ++ j)
		{
			double new_d = dis(p[tmp[i]], p[tmp[j]]);
			d=min(new_d,d);
			if(d<ass)
			{
				ass=d;
				pos1 = tmp[i];
				pos2 = tmp[j];
			}
		}		
	return d;
}

int main()
{
// 	freopen("1.in", "r", stdin);
	scanf("%d", &n);
	for(int i = 1; i <= n; ++ i)	scanf("%lf%lf", &p[i].x, &p[i].y);
	sort(p + 1, p + 1 + n, cmp1);
	ass=1e18;
	double ans = solve(1, n);
	if(p[pos1].x + p[pos1].y > p[pos2].x + p[pos2].y)	swap(pos1, pos2);
	printf("(%.2f,%.2f),(%.2f,%.2f),miniDist=%.3f", p[pos1].x, p[pos1].y, p[pos2].x, p[pos2].y, ans);
	return 0;
}
文章来源:https://blog.csdn.net/weixin_61426225/article/details/135397813
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。