算法与数据结构第四次作业——队列

算法与数据结构实验题 4.15 OJ 队列

★实验任务

设某 OJ 有 A、B 两个评测窗口,且处理业务的速度不一样,其中 A 窗口评测的速度是B 窗口的 2 倍 —— 即当 A 窗口每评测完 2 份代码时,B 窗口评测完 1 份代码。给定评测代码序列,请按 judge 完成的顺序输出代码序列。假定不考虑每份代码先后提交的时间间隔,并且当不同窗口同时处理完 2 份代码时,A 窗口代码编号优先输出,假设提交的代码没有优劣之分。

★数据输入

输入为一行正整数,其中第 1 个数字 N(≤1000)为顾客总数。

下一行为 N 份代码的编号。编号为奇数的代码需要到 A 窗口进行评测,为偶数的代码则去 B 窗口。数字间以空格分隔。

★数据输出

按业务处理完成的顺序输出代码的编号。数字间以空格分隔,但最后一个编号后不能有多余的空格。

输入示例

8
2 1 3 9 4 11 12 15

输出示例

1 3 2 9 11 4 15 12

★hint

输入示例中,由于不考虑代码先后提交的时间间隔,即 编号为 2 的代码放入 B 窗口,当 2 编号的代码评测结束时,编号为 1、3 的代码也在 A 窗口评测结束,之后优先输出 A 窗口的代码。

#include<iostream>
#include<queue>
using namespace std;
int main()
{
	int n,num;
	cin >> n;
	queue<int> A, B;
	for (int i = 0; i < n; i++)
	{
		cin >> num;
		if (num % 2 == 0)
		{
			B.push(num);
		}
		else
		{
			A.push(num);
		}
	}
	int count = 0;
	while (!A.empty() && !B.empty())
	{
		cout << A.front();
		A.pop();
		count++;
		if (!A.empty()||!B.empty()) cout << ' ';
		if (count == 2)
		{
			cout << B.front();
			B.pop();
			if (!B.empty()||!A.empty()) 
				cout << ' ';
			count = 0;
		}
	}
	while (!A.empty())
	{
		cout << A.front();
		A.pop();
		if (!A.empty()) cout << ' ';
	}
	while (!B.empty())
	{
		cout << B.front();
		B.pop();
		if (!B.empty())
		{
			cout << ' ';
		}
	}
}

算法与数据结构实验题 4.8 干净的序列

★实验任务

有一个长度为 n 的序列,第 i 个的数为 a[i]。我们定义如果它的一个连续的子串为“没有进行过干净的交易”,那么这个子串的最大值与最小值之差在[m,k]中。

现在,你的任务是找到一条最长的“没有进行过干净的交易”的子串。

★数据输入

第一行输入三个正整数 n m k (0<n<=10000),m(0 =< m =< k =< n)。

接下去一行有 n 个正整数,第 i 个数表示 a[i]的值。

★数据输出

输出一个数,表示这个子串的长度。

输入示例1

5 0 0
1 1 1 1 1

输出示例1

5

输入示例2

6 0 3
3 1 2 3 4 5

输出示例2

5

很暴力的一种方法,用双循环遍历所有字串,找出最大的符合条件的字串

#include<iostream>
#include<queue>
using namespace std;
int main()
{
	int n, m, k;
	cin >> n >> m >> k;
	int* a = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	int max, min, sum=0, temp,diff;
	queue<int> b;
	for (int i = 0; i < n; i++)
	{
		min = max = a[i];
		for (int j = i + 1; j < n; j++)
		{
			if (a[j] > max)
			{
				max = a[j];
			}
			else if (a[j] < min)
			{
				min = a[j];
			}
			diff = max - min;
            if(diff>k) break;
            if(diff<m) continue;
			if (diff >= m && diff <= k)
			{
				temp= j - i + 1;
				if (temp > sum) sum = temp;
			}
		}
	}
	cout << sum;
}

posted @ 2020-10-04 14:55  与谁  阅读(201)  评论(0编辑  收藏  举报