DSOJ 期末复习【1】线性结构

【id:82】【7分】C. DS顺序表之循环移位

题目描述

顺序表的移位是循环移位,例如顺序表:1,2,3,4,5,6。如果左移1位,即原来的头元素移动到末尾,其它元素向左移1位,变成2,3,4,5,6,1。同理,如果右移1位,即原来的尾元素移动到头,其它元素向右移1位,变成6,1,2,3,4,5。以下是移位的多个例子:

原数据:1,2,3,4,5,6

左移3位:4,5,6,1,2,3,与原数据对比

右移4位:3,4,5,6,1,2,与原数据对比

请编写程序实现顺序表的循环移位操作

输入

第1行输入n表示顺序表包含的·n个数据

第2行输入n个数据,数据是小于100的正整数

第3行输入移动方向和移动的位数,左移方向为0,右移方向为1

第4行输入移动方向和移动的位数,左移方向为0,右移方向为1

注意:移动操作是针对上一次移动后的结果进行的

输出

第一行输出创建后,顺序表内的所有数据,数据之间用空格隔开

第二行输出第一次移位操作后,顺序表内的所有数据,数据之间用空格隔开

第三行输出第二次移位操作后,顺序表内的所有数据,数据之间用空格隔开

这道题是对一个顺序表进行操作,并且分为左右两个方向。很容易发现这是一个头尾相接的环形结构,因此可以使用环形链表进行模拟。

相比较直接用数组进行操作,使用结构体更加直观。

我们首先创建一个结构体,包含before和next两个指针,分别对左右两个方向进行操作。

struct node {
	int data;
	node* before;
	node* next;
}*root;

先创建一个指针保存根节点的副本。

最后再把链表的最后一个元素的next指向刚才创建好的根节点副本就形成了一个环形链表。

根据左右移动的距离,移动指向根节点的指针,最后输出链表的所有结点即可。

AC代码:

#include<string>
#include<stdio.h>
#include<iostream>
#include<vector>

using namespace std;
struct node {
	int data;
	node* before;
	node* next;
}*root;

int main() {
	int n,m,p;
	cin >> n;
	root = new node;
	node *copy = root;
	for (int i = 0; i < n; i++) {
		int num;
		cin >> num;
		root->data = num;
		node* tmp = root;
		if (i == n - 1)
			root->next = copy;
		else
			root->next = new node;
		root = root->next;
		root->before = tmp;
	}
	for(int i=0;i<n;i++) {
		cout << root->data << ' ';
		root = root->next;
	}
	cout << endl;
	cin >> m >> p;
	if (m == 0) {
		for (int i = 0; i < p; i++)
		{
			root = root->next;
		}
		for (int i = 0; i < n; i++) {
			cout << root->data << ' ';
			root = root->next;
		}
		cout << endl;
	}
	else if (m == 1) {
		for (int i = 0; i < n - p % n; i++) {
			root = root->next;
		}
		for (int i = 0; i < n; i++) {
			cout << root->data << ' ';
		}
		cout << endl;
	}
	cin >> m >> p;
	if (m == 0) {
		for (int i = 0; i < p; i++)
		{
			root = root->next;
		}
		for (int i = 0; i < n; i++) {
			cout << root->data << ' ';
			root = root->next;
		}
		cout << endl;
	}
	else if (m == 1) {
		for (int i = 0; i < p; i++) {
			root = root->before;
		}
		for (int i = 0; i < n; i++) {
			cout << root->data << ' ';
			root = root->next;
		}
		cout << endl;
	}
	return 0;
}

struct node {
	int data;
	node* before;
	node* next;
}*root;

【id:81】【7分】D. DS单链表--存储结构与操作

题目描述

用C++实现含头结点的单链表,然后实现单链表的两个结点交换位置。

注意不能简单交换两个结点包含数据,必须通过修改指针来实现两个结点的位置交换

交换函数定义可以参考:

swapNode(int  pa, int pb)  //pa和pb表示两个结点在单链表的位置序号

swapNode (ListNode * p, ListNode * q)  //p和q表示指向两个结点的指针

输入

第1行先输入n表示有n个数据,接着输入n个数据

第2行输入要交换的两个结点位置

第3行输入要交换的两个结点位置

输出

第一行输出单链表创建后的所有数据,数据之间用空格隔开

第二行输出执行第1次交换操作后的单链表数据,数据之间用空格隔开

第三行输出执行第2次交换操作后的单链表数据,数据之间用空格隔开

如果发现输入位置不合法,输出字符串error,不必输出单链表

AC代码:

#include<string>
#include<stdio.h>
#include<iostream>
#include<vector>

using namespace std;
struct node {
	int data;
	node* next=NULL;
}*head;

int main() {
	int n,m,num;
	cin >> n;
	head = new node;
	node* copy = head;
	for (int i = 0; i < n; i++) {
		cin >> num;
		head->next = new node;
		head = head->next;
		head->data = num;
	}
	head = copy;
	while (head->next) {
		head = head->next;
		cout << head->data << ' ';
	}
	cout << endl;
	head = copy;
	n = 2;
	while (n--) {
		cin >> m >> num;
		int j = 1;
		while (head->next && j < m) {
			head = head->next;
			j++;
		}
		if (j != m)
		{	
		cout << "error" << endl;
		head = copy;
		continue;
		}
		node* tmp =	new node;
		tmp->data = num;
		tmp->next = head->next;
		head->next = tmp;
		head = copy->next;
		while (head) {
			cout << head->data<<' ';
			head = head->next;
		}
		cout << endl;
		head = copy;
	}
	n = 2;
	while (n--) {
		cin >> m;
		int j = 1;
		while (head->next && j < m) {
			head = head->next;
			j++;
		}
		if (!head->next||j != m)
		{
			cout << "error" << endl;
			head = copy;
			continue;
		}
		head->next = head->next->next;
		head = copy->next;
		while (head) {
			cout << head->data << ' ';
			head = head->next;
		}
		cout << endl;
		head = copy;
	}
	n = 2;
	while (n--) {
		cin >> m;
		int j = 1;
		while (head->next && j < m) {
			head = head->next;
			j++;
		}
		if (!head->next || j != m) {
			cout << "error" << endl;
			head = copy;
			continue;
		}
		cout << head->next->data << endl;
		head = copy;
	}
	return 0;
}



【id:80】【7分】E. DS单链表--结点交换

内存限制128MB

题目描述

用C++实现含头结点的单链表,然后实现单链表的两个结点交换位置。

注意不能简单交换两个结点包含数据,必须通过修改指针来实现两个结点的位置交换

交换函数定义可以参考:

swapNode(int  pa, int pb)  //pa和pb表示两个结点在单链表的位置序号

swapNode (ListNode * p, ListNode * q)  //p和q表示指向两个结点的指针

输入

第1行先输入n表示有n个数据,接着输入n个数据

第2行输入要交换的两个结点位置

第3行输入要交换的两个结点位置

输出

第一行输出单链表创建后的所有数据,数据之间用空格隔开

第二行输出执行第1次交换操作后的单链表数据,数据之间用空格隔开

第三行输出执行第2次交换操作后的单链表数据,数据之间用空格隔开

如果发现输入位置不合法,输出字符串error,不必输出单链表

AC代码:

#include<string>
#include<stdio.h>
#include<iostream>
#include<vector>

using namespace std;
struct node {
	int data;
	node* next=NULL;
}*head;
int swap(int pa, int pb, node* head) {
	node* copy = head;
	int j = 1;
	while (head->next && j < pa) {
		head = head->next;
		j++;
	}
	if (!head->next||j != pa) {
		cout << "error" << endl;
		return 0;
	}
	node* tmp1 = head;
	head = copy;
	j = 1;
	while (head->next && j<pb) {
		head = head->next;
		j++;
	}
	if (!head->next||j != pb ) {
		cout << "error" << endl;
		return 0;
	}
	node* tmp2 = tmp1->next;
	tmp1->next = tmp2->next;
	tmp2->next = head->next;
	head->next = tmp2;
	node *tmp3 = tmp2->next;
	tmp2->next = tmp3->next;
	tmp3->next = tmp1->next;
	tmp1->next = tmp3;
	return 1;
}
int main() {
	int n,m,num;
	cin >> n;
	head = new node;
	node* copy = head;
	for (int i = 0; i < n; i++) {
		cin >> num;
		head->next = new node;
		head = head->next;
		head->data = num;
	}
	head = copy;
	while (head->next) {
		head = head->next;
		cout << head->data << ' ';
	}
	cout << endl;
	n = 2;
	int pa, pb;
	while (n--) {
		head = copy;
		cin >> pa >> pb;
		if (swap(pa, pb, head)) {
			while (head->next) {
				head = head->next;
				cout << head->data << ' ';
			}
			cout << endl;
		}
	}
	
	return 0;
}



【id:79】【7分】F. DS单链表--合并

题目描述

假定两个单链表是递增有序,定义并实现以下函数,完成两个单链表的合并,继续保持递增有序

int LL_merge(ListNode *La, ListNode *Lb)

输入

第1行先输入n表示有n个数据,接着输入n个数据

第2行先输入m表示有M个数据,接着输入m个数据

输出

输出合并后的单链表数据,数据之间用空格隔

AC代码:

合并的时候由于两个都是升序,所以最小的根节点就是总链表的根节点。

合并的时候每次找出较小的序列中不大于另外序列的最大结点,进行插入。重复操作直到某个序列变空。

#include<string>
#include<stdio.h>
#include<iostream>
#include<vector>

using namespace std;
struct node {
	int data;
	node* next=NULL;
}*head1,*head2;
node *merge(node* head1, node* head2) {
	node* root;
	head1 = head1->next;
	head2 = head2->next;
	root = head1->data < head2->data ? head1 : head2;
	while (head1 && head2) {
		if (head1->data >= head2->data) {
			while (head2->next&&head1->data > head2->next->data) {
				head2 = head2->next;
			}
			node *tmp = head2->next;
			head2->next = head1;
			head2 = tmp;
		}
		else if (head1->data < head2->data) {
			while (head1->next&&head2->data > head1->next->data) {
				head1 = head1->next;
			}
			node* tmp = head1->next;
			head1->next = head2;
			head1 = tmp;
		}
	}
	return root;
 }
int main() {
	int n,m,num;
	cin >> n;
	head1 = new node;
	node* copy1 = head1;
	for (int i = 0; i < n; i++) {
		cin >> num;
		head1->next = new node;
		head1 = head1->next;
		head1->data = num;
	}
	cin >> n;
	head2 = new node;
	node* copy2 = head2;
	for (int i = 0; i < n; i++) {
		cin >> num;
		head2->next = new node;
		head2 = head2->next;
		head2->data = num;
	}
	head1 = copy1, head2 = copy2;
	node* root;
	root = merge(head1, head2);
	while (root) {
		cout << root->data << ' ';
		if (root->next)
			root = root->next;
		else
			break;
	}
	return 0;
}



【id:78】【7分】G. DS线性表—多项式相加

题目描述

对于一元多项式p(x)=p0+p1x+p2x2++pnxn,每个项都有系数和指数两部分,例如p2x2的系数为p2,指数为2。

编程实现两个多项式的相加。

例如5+x+2x2+3x3-5-x+6x2+4x4,两者相加结果:8x2+3x3+4x4

其中系数5和-5都是x的0次方的系数,相加后为0,所以不显示。x的1次方同理不显示。

要求用单链表实现。

输入

第1行:输入t表示有t组测试数据

第2行:输入n表示有第1组的第1个多项式包含n个项

第3行:输入第一项的系数和指数,以此类推输入n行

接着输入m表示第1组的第2个多项式包含m项

同理输入第2个多项式的m个项的系数和指数

参考上面输入第2组数据,以此类推输入t组

假设所有数据都是整数

输出

对于每1组数据,先用两行输出两个原来的多项式,再用一行输出运算结果,不必考虑结果全为0的情况

输出格式参考样本数据,格式要求包括:

1.如果指数或系数是负数,用小括号括起来。

2.如果系数为0,则该项不用输出。

3.如果指数不为0,则用符号^表示,例如x的3次方,表示为x^3。

4.多项式的每个项之间用符号+连接,每个+两边加1个空格隔开。

AC代码: 

通过三个数组分别存两组指数和系数,以及一个总和。

输出的时候注意如果之后所有数组为空就输出加号。

由于有负指数,把指数+5存入数组,输出的时候把脚标减去5就行了。

#include<string>
#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;


void display(int* list) {
	for (int i = 0; i < 15; i++) {
		if (list[i]) {
			if (i <= 4)
				if (list[i] > 0)
					cout << list[i] << "x^(" << i - 5 << ")" ;
				else
					cout << "(" << list[i] << ")x^(" << i - 5 << ")";
			else if (i == 5)
				if (list[i] > 0)
					cout << list[i];
				else
					cout << "(" << list[i] << ")";
			else
				if (list[i] > 0)
					cout << list[i] << "x^" << i - 5 ;
				else
					cout << "(" << list[i] << ")x^" << i - 5 ;
			for (int j = i + 1; j < 15; j++)
			{
				if (list[j])
				{
					cout << " + ";
					break;
				}

			}
		}
	}
	cout << endl;
}

int main(){
	int t;
	cin >> t;
	int n;
	int p, q;
	int list[15];
	int list1[15];
	int list2[15];
	while (t--) {
		for (int i = 0; i < 15; i++)
		{
			list1[i] = 0;
			list[i] = 0;
			list2[i] = 0;
		}
		cin >> n;
		for (int i = 0; i < n; i++) {
			cin >> p >> q;
			list1[q+5] += p;
			list[q + 5] += p;
		}
		cin >> n;
		for (int i = 0; i < n; i++) {
			cin >> p >> q;
			list2[q+5] += p;
			list[q + 5] += p;
		}
		display(list1);
		display(list2);
		display(list);
		
	}

	return 0;
}



【id:129】【16分】H. DS堆栈--逆序输出(STL栈使用)

题目描述

C++中已经自带堆栈对象stack,无需编写堆栈操作的具体实现代码。

本题目主要帮助大家熟悉stack对象的使用,然后实现字符串的逆序输出

输入一个字符串,按字符按输入顺序压入堆栈,然后根据堆栈后进先出的特点,做逆序输出

stack类使用的参考代码

n包含头文件<stack>:#include <stack>

n创建一个堆栈对象s(注意stack是模板类):stack <char>  s;//堆栈的数据类型是字符型

n把一个字符ct压入堆栈:s.push(ct);

n把栈顶元素弹出:s.pop();

n获取栈顶元素,放入变量c2:c2 =s.top();

n判断堆栈是否空:s.empty(),如果为空则函数返回true,如果不空则返回false

输入

第一行输入t,表示有t个测试实例
第二起,每一行输入一个字符串,注意字符串不要包含空格

字符串的输入可以考虑一下代码:

#include <string>

int main()

{ string str;

Int len;

cin>>str; //把输入的字符串保存在变量str中

len = str.length()  //获取输入字符串的长度

}

输出

每行逆序输出每一个字符串

AC代码:

#include<string>
#include<stdio.h>
#include<iostream>
#include<vector>
#include<stack>
using namespace std;


int main(){
	int t;
	cin >> t;
	while (t--) {
		string ss;
		cin >> ss;
		stack<char>s;
		for (int i = 0; i < ss.length(); i++) {
			s.push(ss[i]);
		}
		while (!s.empty()) {
			char c = s.top();
			s.pop();
			cout << c;
		}
		cout << endl;
	}

	return 0;
}



【id:127】【7分】I. DS堆栈--行编辑

题目描述

使用C++的STL堆栈对象,编写程序实现行编辑功能。行编辑功能是:当输入#字符,则执行退格操作;如果无字符可退就不操作,不会报错

本程序默认不会显示#字符,所以连续输入多个#表示连续执行多次退格操作

每输入一行字符打回车则表示字符串结束

注意:必须使用堆栈实现,而且结果必须是正序输出

输入

第一行输入一个整数t,表示有t行字符串要输入
第二行起输入一行字符串,共输入t行

输出

每行输出最终处理后的结果,如果一行输入的字符串经过处理后没有字符输出,则直接输出NULL

 

AC代码:

注意stack是逆序输出,再用一个stack就可以顺序输出了。 

#include<string>
#include<stdio.h>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;


int main(){
	int t;
	cin >> t;
	while (t--) {
		string ss;
		cin >> ss;
		stack<char>s,s2;
		for (int i = 0; i < ss.length(); i++) {
			if (ss[i] == '#') {
				if (!s.empty())
					s.pop();
			}
			else
			s.push(ss[i]);
		}
		if (s.empty()) {
			cout << "NULL" << endl;
		}
		else
		{
			while (!s.empty()) {
				char c = s.top();
				s.pop();
				s2.push(c);
			}
			while (!s2.empty()) {
				char c = s2.top();
				s2.pop();
				cout << c;
			}
			cout << endl;
		}
	}

	return 0;
}



【id:126】【7分】J. DS堆栈--括号匹配

时间限制1s

内存限制128MB

题目描述

处理表达式过程中需要对括号匹配进行检验,括号匹配包括三种:“(”和“)”,“[”和“]”,“{”和“}”。例如表达式中包含括号如下:

(	)	[	(	)	(	[	]	)	]	{	}
1	2	3	4	5	6	7	8	9	10	11	12

从上例可以看出第1和第2个括号匹配,第3和第10个括号匹配,4和5匹配,6和9匹配,7和8匹配,11和12匹配。从中可以看到括号嵌套的的情况是比较复杂的,使用堆栈可以很方便的处理这种括号匹配检验,可以遵循以下规则:

1、 当接收第1个左括号,表示新的一组匹配检查开始;随后如果连续接收到左括号,则不断进堆栈。

2、 当接受第1个右括号,则和最新进栈的左括号进行匹配,表示嵌套中1组括号已经匹配消除

3、 若到最后,括号不能完全匹配,则说明输入的表达式有错

建议使用C++自带的stack对象来实现

stack类使用的参考代码

n包含头文件<stack>:#include <stack>

n创建一个堆栈对象s(注意stack是模板类):stack <char>  s;//堆栈的数据类型是字符型

n把一个字符ct压入堆栈:s.push(ct);

n把栈顶元素弹出:s.pop();

n获取栈顶元素,放入变量c2:c2 =s.top();

n判断堆栈是否空:s.empty(),如果为空则函数返回true,如果不空则返回false

输入

第一行输入一个t,表示下面将有t组测试数据。接下来的t行的每行输入一个表达式,表达式只考虑英文半角状态输入,无需考虑中文全角输入

输出

对于每一行的表达式,检查括号是否匹配,匹配则输入ok,不匹配则输出error

 AC代码:

#include<string>
#include<stdio.h>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;


int main(){
	int t;
	cin >> t;
	
	while (t--) {
		stack<char>s;
		string ss;
		cin >> ss;
		for (auto &it:ss) {
			if (it == '(' || it == '[' || it == '{')s.push(it);
			else if ((it == ')' || it == ']' || it == '}') && s.empty()) {
				s.push(it);
				break;
			}
			else if (it == ')' && s.top() == '(' || it == ']' && s.top() == '[' || it == '}' && s.top() == '{')
				s.pop();
		}
		if (s.empty())
			cout << "ok" << endl;
		else cout << "error" << endl;
	}
	return 0;
}



【id:122】【7分】K. DS队列+堆栈--数制转换

时间限制1s

内存限制128MB

题目描述

对于任意十进制数转换为k进制,包括整数部分和小数部分转换。整数部分采用除k求余法,小数部分采用乘k取整法例如x=19.125,求2进制转换

整数部分19,					小数部分0.125
19 / 2 = 9 … 1					0.125 * 2 = 0.25 … 0
9 / 2 = 4 … 1					0.25 * 2 = 0.5   … 0
4 / 2 = 2 … 0 					0.5 * 2 = 1     … 1
2 / 2 = 1 … 0
1 / 2 = 0 … 1

所以整数部分转为 10011,小数部分转为0.001,合起来为10011.001

提示整数部分可用堆栈,小数部分可用队列实现

注意:必须按照上述方法来实现数制转换,其他方法0分

输入

第一行输入一个t,表示下面将有t组测试数据。

接下来每行包含两个参数n和k,n表示要转换的数值,可能是非整数;k表示要转换的数制,1<k<=16

输出

对于每一组测试数据,每行输出转换后的结果,结果精度到小数点后3位

输出小数点后几位的代码如下:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
double r = 123.56789;
cout<<fixed<<setprecision(4)<<r<<endl;   //输出小数点后4

return 0;
}

Tips:注意要保留三位小数。同时有可能小数部分可能无法用3位表示。用一个bits直接截取3位就好了。

AC代码:

#include<string>
#include<stdio.h>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;

int main() {
	int t;
	double num;
	int n;
	cin >> t;
	while(t--){
	
	cin >> num >> n;
	int integer = num / 1;
	double decimal = num - integer;
	queue<int>q;
	stack<int>s;
	if (integer == 0)
		s.push(0);
	else
	{
		while (integer) {
			s.push(integer % n);
			integer /= n;
		}
	}
	int bits = 1;
	while (decimal != 1&&bits<=3) {
		int m = decimal * n;
		q.push(m%n);
		decimal =decimal*n-m;
		bits++;
	}
	while (!s.empty()) {
		int m = s.top();
		if (m >= 10)
		{
			char c = 'A' + m - 10;
			cout << c;
		}
		else cout << m;
		s.pop();
	}
	cout << '.';
	while (!q.empty()) {
		int m = q.front();
		if (m >= 10)
		{
			char c = 'A' + m - 10;
			cout << c;
		}
		else cout << m;
		q.pop();
	}
	cout << endl;
}


	return 0;
}



【id:123】【7分】L. DS队列之银行排队

时间限制1s

内存限制128MB

题目描述

在银行营业大厅共服务3种客户,类型为A\B\C,大厅分别设置了3个窗口分别服务三种客户,即每个窗口只服务一种客户。现有一批客户来银行办理业务,每个客户都有类型和办理业务时间。每个窗口按照客户到来的顺序进行服务。

编程实现它们的办理流程,请使用C++自带的queue必须使用队列实现,其他方法0分!

队列queue的用法如下:

1.包含头文件:#include <queue>

2.定义一个整数队列对象:queue<int>  myQe;

3.定义一个整数队列对象数组:queue<int>  myQA[10];

4.入队操作:myQe.push(itemp); //把整数itemp进入队列

5.出队操作:myQe.pop();  //把队头元素弹出队列,注意本操作不获取队头元素

6.获取队头元素: itemp = myQe.front(); // 把队头元素放入itemp中,注意本操作不弹出元素

7.判断队列是否为空:myQe.empty();//队列空则返回true,不空则返回false

输入

第一行输入先输入n表示客户数量

第二行输入每个客户的类型,数据之间用用空格隔开

第三行输入每个客户的办理时间,数据之间用用空格隔开

输出

第一行输出A类客户的平均办理时间

第二行输出B类客户的平均办理时间

第三行输出C类客户的平均办理时间

AC代码:

#include<string>
#include<stdio.h>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;

int main() {
	queue<int>q[2];
	int n;
	cin >> n;
	for(int i=0;i<n;i++){
		char c;
		cin >> c;
		q[0].push(c);
	}
	for (int i = 0; i < n; i++) {
		int num;
		cin >> num;
		q[1].push(num);
	}
	int cnt1=0, cnt2=0, cnt3=0, num1=0, num2=0, num3=0;
	while (n--) {
		char c = q[0].front();
		int num = q[1].front();
		if (c == 'A') {
			num1 += num;
			cnt1++;
		}
		else if (c == 'B')
		{
			num2 += num;
			cnt2++;
		}
		else if (c == 'C') {
			num3 += num;
			cnt3++;
		}
		q[0].pop();
		q[1].pop();
	}
	cout << num1 / cnt1 << endl << num2 / cnt2 << endl << num3 / cnt3 << endl;
	return 0;
}



【id:120】【7分】M. DS栈+队列—排队游戏

时间限制1s

内存限制128MB

题目描述

在幼儿园中,老师安排小朋友做一个排队的游戏。首先老师精心的把数目相同的小男孩和小女孩编排在一个队列中,每个小孩按其在队列中的位置发给一个编号(编号从0开始)。然后老师告诉小朋友们,站在前边的小男孩可以和他后边相邻的小女孩手拉手离开队列,剩余的小朋友重新站拢,再按前后相邻的小男孩小女孩手拉手离开队列游戏,如此往复。由于教师精心的安排,恰好可以保证每两个小朋友都能手拉手离开队列,并且最后离开的两个小朋友是编号最小的和最大的两个小朋友。(注:只有小男孩在前,小女孩在后,且他们两之间没有其他的小朋友,他们才能手拉手离开队列)。请根据老师的排队,按小女孩编号从小到大的顺序,给出所有手拉手离开队列的小男孩和小女孩的编号对。

输入

用一个字符串代表小朋友队列。字符串中只会出现两个字符,分别代表小男孩和小女孩,首先出现的字符代表小男孩,另一个字符代表小女孩。小孩总数不超过2000。

输出

按小女孩编号顺序,顺序输出手拉手离开队列的小男孩和小女孩的编号对,每行一对编号,编号之间用一个空格分隔。


为了记录小男孩和小女孩的位置用了一个类

stack和queue都是可以用类作为数据类型的

把字符串首个字符设置为小男孩。检查字符串每个字符,如果是小男孩就压入栈中,如果不是那么就是小女孩,需要弹出,同时输出各自位置就行了

#include<string>
#include<stdio.h>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
class child {
public:
	int num;
	char c;
	child(char _c, int _num)
	{
		c = _c, num = _num;
	}
};
int main() {
	string ss;
	cin >> ss;
	stack<child>q;
	char boy = ss[0];
	for (int i = 0; i < ss.length(); i++) {
			child ch(ss[i],i);
			if (ch.c == boy)
				q.push(ch);
			else {
				child ch2 = q.top();
				cout << ch2.num << ' ' << ch.num << endl;
				q.pop();
			}
	}
	return 0;
}



  • 18
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值