郑州大学算法设计与分析实验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;
}
相关推荐
PieroPc17 分钟前
Python 自动化 打开网站 填表登陆 例子
运维·python·自动化
是小胡嘛22 分钟前
数据结构之旅:红黑树如何驱动 Set 和 Map
数据结构·算法
m0_7482550226 分钟前
前端常用算法集合
前端·算法
FF在路上33 分钟前
Knife4j调试实体类传参扁平化模式修改:default-flat-param-object: true
java·开发语言
真的很上进40 分钟前
如何借助 Babel+TS+ESLint 构建现代 JS 工程环境?
java·前端·javascript·css·react.js·vue·html
呆呆的猫1 小时前
【LeetCode】227、基本计算器 II
算法·leetcode·职场和发展
Tisfy1 小时前
LeetCode 1705.吃苹果的最大数目:贪心(优先队列) - 清晰题解
算法·leetcode·优先队列·贪心·
VinciYan1 小时前
基于Jenkins+Docker的自动化部署实践——整合Git与Python脚本实现远程部署
python·ubuntu·docker·自动化·jenkins·.net·运维开发
余额不足121381 小时前
C语言基础十六:枚举、c语言中文件的读写操作
linux·c语言·算法
众拾达人1 小时前
Android自动化测试实战 Java篇 主流工具 框架 脚本
android·java·开发语言