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

判断题


1

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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;
}
相关推荐
吾日三省吾码14 分钟前
JVM 性能调优
java
LNTON羚通43 分钟前
摄像机视频分析软件下载LiteAIServer视频智能分析平台玩手机打电话检测算法技术的实现
算法·目标检测·音视频·监控·视频监控
湫ccc1 小时前
《Python基础》之字符串格式化输出
开发语言·python
弗拉唐1 小时前
springBoot,mp,ssm整合案例
java·spring boot·mybatis
oi772 小时前
使用itextpdf进行pdf模版填充中文文本时部分字不显示问题
java·服务器
mqiqe2 小时前
Python MySQL通过Binlog 获取变更记录 恢复数据
开发语言·python·mysql
AttackingLin2 小时前
2024强网杯--babyheap house of apple2解法
linux·开发语言·python
少说多做3432 小时前
Android 不同情况下使用 runOnUiThread
android·java
知兀2 小时前
Java的方法、基本和引用数据类型
java·笔记·黑马程序员