front
和rear
的值分别为0和4。当从队列中删除两个元素,再加入两个元素后,front
和rear
的值分别为多少? (A)
A.2和0 B.2和2 C.2和4 D.2和6
front
和rear
的值分别为0和4,又数组大小为6,所以值的范围为[0,5],先删除两个元素,在添加两个元素,front
与rear
的值分别加2,为2,0.m
的数组表示,且用队头指针front
和队列元素个数size
代替一般循环队列中的front
和rear
指针来表示队列的范围,那么这样的循环队列可以容纳的元素个数最多为:
A.m-1 B. m C.m+1 D.不能确定
4-1 栈的运算遵循 后进先出
4-2 以下运算实现在链队上的入队列,请在空白处用适当句子予以填充。
void EnQueue(QueptrTp *lq,DataType x){
LqueueTp *p;
p=(LqueueTp *)malloc(sizeof(LqueueTp))
p->data=x; // 1分
p->next=NULL;
(lq->rear)->next=p// 1分;
lq->rear=p // 1分;
}
4-3 以下运算实现在链栈上的初始化,请在空白处用请适当句子予以填充。
typedef struct Node{
DataType data;
struct Node *next;
}StackNode,*LStackTp;
void InitStack(LStackTp &ls){
ls=NULL
}//3分;
假设在周末舞会上,男士和女士们分别进入舞厅,各自排成一队。跳舞开始,依次从男队和女队队头各出一人配成舞伴,若两队初始人数不同,则较长那一队未配对者等待下一轮舞曲。现要求写一算法模拟上述舞伴配对问题。 你需要用队列操作实现上述算法。请完成下面5个函数的操作。
int QueueLen(SqQueue Q);//队列长度
int EnQueue(SqQueue &Q, Person e);//加入队列
int QueueEmpty(SqQueue &Q);//队列是否为空
int DeQueue(SqQueue &Q, Person &e);//出队列
void DancePartner(Person dancer[], int num); //配对舞伴
Q
:队列e
:参加舞会的人dancer
:全部舞者num
:参加舞会的人数**输入说明 先输入参加舞会人数,再分别输入参加舞会人的姓名和性别 **
**输出说明 先输出配对的男女舞伴,若队伍有剩人,则输出剩下人性别及剩下人数目。 **
#include<iostream>
#define MAXQSIZE 100//队列可能达到的最大长度
#define OK 1
#define ERROR 0
#define OVERFLOW -2
using namespace std;
typedef struct {
char name[20]; //姓名
char sex; //性别,'F'表示女性,'M'表示男性
} Person;
//- - - - - 队列的顺序存储结构- - - - -
typedef struct {
Person data[MAXQSIZE];
int front; //头指针
int rear; //尾指针
} Queue;
typedef Queue *SqQueue;
SqQueue Mdancers, Fdancers; //分别存放男士和女士入队者队列
int InitQueue(SqQueue &Q);
void DestroyQueue(SqQueue &q);
int QueueLen(SqQueue Q);//队列长度
int EnQueue(SqQueue &Q, Person e);//加入队列
int QueueEmpty(SqQueue &Q);//队列是否为空
int DeQueue(SqQueue &Q, Person &e);//出队列
void DancePartner(Person dancer[], int num); //配对舞伴
int main(){
int i;
int n;
Person dancer[MAXQSIZE];
cin>>n;
for(i=0;i<n;i++) cin>> dancer[i].name >> dancer[i].sex;
InitQueue(Mdancers); //男士队列初始化
InitQueue(Fdancers); //女士队列初始化
cout << "The dancing partners are:" << endl;
DancePartner(dancer, n);
if (!QueueEmpty(Fdancers)) {
cout << "F:"<<QueueLen(Fdancers) ;
} else if (!QueueEmpty(Mdancers)) {
cout << "M:"<<QueueLen(Mdancers) ;
}
DestroyQueue(Fdancers);
DestroyQueue(Mdancers);
return 0;
}
int InitQueue(SqQueue &Q) {//构造一个空队列Q
Q = new Queue; //为队列分配一个最大容量为MAXSIZE的数组空间
if (!Q->data)
exit( OVERFLOW); //存储分配失败
Q->front = Q->rear = 0; //头指针和尾指针置为零,队列为空
return OK;
}
void DestroyQueue(SqQueue &q)
{
delete q;
}
/* 请在这里填写答案 */
int QueueLen(SqQueue Q)
{
return (Q->rear - Q->front + MAXQSIZE ) % MAXQSIZE;
};//队列长度
int EnQueue(SqQueue &Q, Person e)
{
Q->rear = (Q->rear + 1)% MAXQSIZE;
Q->data[Q->rear] = e;
return 0;
};//加入队列
int QueueEmpty(SqQueue &Q)
{
if(Q->rear==Q->front)
{
return 1;
}
else return 0;
};//队列是否为空
int DeQueue(SqQueue &Q, Person &e)
{
Q->front=(Q->front+1)%MAXQSIZE;
e=Q->data[Q->front];
return 0;
};//出队列
void DancePartner(Person dancer[], int num)
{
for (int i=0; i<num; i++)
{
if(dancer[i].sex=='F')
{
EnQueue(Fdancers,dancer[i]);
}
if(dancer[i].sex=='M')
{
EnQueue(Mdancers,dancer[i]);
}
}
while(QueueEmpty(Mdancers)!=1&&QueueEmpty(Fdancers)!=1)
{
Person x,y;
DeQueue(Mdancers, x);
DeQueue(Fdancers, y);
cout<<y.name<<" "<<x.name<<endl;
}
}; //配对舞伴
设计一个顺序栈,并利用该顺序栈将给定的十进制整整数转换为二进制并输出。
#define MaxSize 100 /* 栈最大容量 */
int top; /* 栈顶指针 */
int mystack[MaxSize]; /* 顺序栈 */
/*判栈是否为空,空返回true,非空返回false */
bool isEmpty();
/* 元素x入栈 */
void Push(int x);
/* 取栈顶元素 */
int getTop();
/* 删除栈顶元素 */
void Pop();
其中 MaxSize
和 top
分别为栈的最大容量和栈顶指针。数组mystack
用来模拟顺序栈。请实现给出的isEmpty
、Push
、getTop
和Pop
这四个函数。
#include <bits/stdc++.h>
using namespace std;
#define MaxSize 100 /* 栈最大容量 */
int top; /* 栈顶指针 */
int mystack[MaxSize]; /* 顺序栈 */
/*判栈是否为空,空返回true,非空返回false */
bool isEmpty();/* 元素x入栈 */
void Push(int x);/* 取栈顶元素 */
int getTop();/* 删除栈顶元素 */
void Pop();/* 十进制正整数转换为二进制 */
void dec2bin(int x) {
top = -1; /* 初始化栈顶指针 */
while (x) {
Push(x % 2);
x >>= 1;
}
while (!isEmpty()) {
int t = getTop();
Pop();
printf("%d", t);
}
printf("\n");
}
int main(int argc, char const *argv[])
{
int n;
while (scanf("%d", &n) != EOF) {
dec2bin(n);
}
return 0;
}
/* 请在这里填写答案 */
bool isEmpty()
{
if (top == -1) return true;
else return false;
}
void Push(int x)
{
if (top == MaxSize - 1)
return;
else mystack[++top] = x;
}
int getTop()
{
if (top == -1) return 0;
else return mystack[top];
}
void Pop()
{
if (top == -1) return;
else top--;
}
设某银行有A、B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 —— 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客。给定到达银行的顾客序列,请按业务完成的顺序输出顾客序列。假定不考虑顾客先后到达的时间间隔,并且当不同窗口同时处理完2个顾客时,A窗口顾客优先输出。
输入为一行正整数,其中第1个数字N(≤1000)为顾客总数,后面跟着N位顾客的编号。编号为奇数的顾客需要到A窗口办理业务,为偶数的顾客则去B窗口。数字间以空格分隔。
按业务处理完成的顺序输出顾客的编号。数字间以空格分隔,但最后一个编号后不能有多余的空格。
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
int main()
{
int A[1000];
int B[1000];
int N;
int a=0, b=0, temp;
scanf("%d", &N);
for(int i=0; i<N; i++){
scanf("%d", &temp);
if(temp % 2 == 1)
A[a++] = temp;
else
B[b++] = temp;
}
int i = 0, j = 0;
if(a > 1){//A有超过两个人,输出两个
printf("%d %d", A[0], A[1]);
i = 2;
}
else if(a > 0)//A有超过一个人,输出一个
printf("%d", A[i++]);
else if(b > 0)//A没人,B有人,输出B一个
printf("%d", B[j++]);
if(j == 0)//输出了A没输出B,输出一个B来控制输出一组
printf(" %d", B[j++]);
while(i < a && j < b){
if(i+1 < a){
printf(" %d %d",A[i], A[i+1]);
i += 2;
}
else
printf(" %d", A[i++]);
printf(" %d", B[j++]);
}
while(i < a)
printf(" %d", A[i++]);
while(j < b)
printf(" %d", B[j++]);
}
假设以`S`和`X`分别表示入栈和出栈操作。如果根据一个仅由`S`和`X`构成的序列,对一个空堆栈进行操作,相应操作均可行(如没有出现删除时栈空)且最后状态也是栈空,则称该序列是合法的堆栈操作序列。请编写程序,输入`S`和`X`序列,判断该序列是否合法。
输入第一行给出两个正整数N和M,其中N是待测序列的个数,M(≤50)是堆栈的最大容量。随后N行,每行中给出一个仅由S
和X
构成的序列。序列保证不为空,且长度不超过100。
对每个序列,在一行中输出YES
如果该序列是合法的堆栈操作序列,或NO
如果不是。
4 10
SSSXXSXXSX
SSSXXSXXS
SSSSSSSSSSXSSXXXXXXXXXXX
SSSXXSXXX结尾无空行
YES
NO
NO
NO结尾无空行
#include <iostream>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
while (N--) {
string a;
int cnt = 0, flag = 1;
cin >> a;
for (int i = 0; i < a.size(); ++i)
if (a[i] == 'S') {
cnt++;
if (cnt > M) {
flag = 0;
cout << "NO" << endl;
break;
}
}
else {
cnt--;
if (cnt < 0) {
flag = 0;
cout << "NO" << endl;
break;
}
}
if (flag)
cout << (cnt == 0 ? "YES" : "NO") << endl;
}
return 0;
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有