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

2024-01-05 23:52

本文主要是介绍郑州大学算法设计与分析实验2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

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()
{IOScin>>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 longusing 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;
}

这篇关于郑州大学算法设计与分析实验2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/574560

相关文章

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

python中Hash使用场景分析

《python中Hash使用场景分析》Python的hash()函数用于获取对象哈希值,常用于字典和集合,不可变类型可哈希,可变类型不可,常见算法包括除法、乘法、平方取中和随机数哈希,各有优缺点,需根... 目录python中的 Hash除法哈希算法乘法哈希算法平方取中法随机数哈希算法小结在Python中,

Java Stream的distinct去重原理分析

《JavaStream的distinct去重原理分析》Javastream中的distinct方法用于去除流中的重复元素,它返回一个包含过滤后唯一元素的新流,该方法会根据元素的hashcode和eq... 目录一、distinct 的基础用法与核心特性二、distinct 的底层实现原理1. 顺序流中的去重

关于MyISAM和InnoDB对比分析

《关于MyISAM和InnoDB对比分析》:本文主要介绍关于MyISAM和InnoDB对比分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录开篇:从交通规则看存储引擎选择理解存储引擎的基本概念技术原理对比1. 事务支持:ACID的守护者2. 锁机制:并发控制的艺

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三

MyBatis设计SQL返回布尔值(Boolean)的常见方法

《MyBatis设计SQL返回布尔值(Boolean)的常见方法》这篇文章主要为大家详细介绍了MyBatis设计SQL返回布尔值(Boolean)的几种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴... 目录方案一:使用COUNT查询存在性(推荐)方案二:条件表达式直接返回布尔方案三:存在性检查(EXI