第一题:
Problem A: 岗位实践2——顺序表的插入
顺序表是采用数组存储数据的一种线性数据结构。实现顺序表的插入操作,即给出指定位置和数据,把该位置和后面的数据往后移动1位,然后再插入数据。假定顺序表的位置从1开始计算。
#include<iostream>
using namespace std;
#define L 50
class list{
private:
int elem[L];
int length;
public:
list()//链表初始化
{
int n,i;
cin>>n;
for(i=0;i<n;i++)
cin>>elem[i];
length=n;
}
int getLength()//获取实际长度
{
return length;
}
bool Insert(int i,int e)//在i的位置插入e这个元素
{
if (i<1 || i>length+1) return false;//判断越界
for (int p=length-1;p>=i-1;--p)
elem[p+1]=elem[p]; // 右移
elem[i-1]=e;
++length;
return true;
}
void print()//打印输出
{
int k;
for(k=0;k<length;k++)
cout<<elem[k]<<" ";
cout<<endl;
}
};
int main(){
//freopen("cin.txt","r",stdin);
int t,e1,n;
list list1;//声明对象
cin>>n;
while(n--)
{
cin>>t>>e1;
list1.Insert(t,e1);
list1.print();
}
return 0;
}
第二题:
Problem B: 岗位实践2-单链表截取
给出一个单链表的两个截取位置实现截取操作。操作过程是:给出截取的首位置和尾位置,然后从单链表中删除首尾之间的链表结点,把剩余部分连接起来形成新链表。单链表采用头结点方式,头结点位置为0,存储首个数据的结点位置为1,所以截取首位置最小是1。
强调:必须使用指针和带头结点的单链表结构实现。
#include <iostream>
using namespace std;
struct LNode
{
int data;
LNode* next;
};
LNode * search(LNode *L,int i)
{
int j;
LNode *p;
p=L;
j=1;
while(p&&j<i)
{
p=p->next;
j++;
}
if(!p||i<=0)
return NULL;
return p;
}
void insert(LNode *L,int elem,int i)
{
LNode *p,*s;
p=search(L,i-1);
if(!p)
return ;
s=new LNode;
s->data=elem;
s->next=p->next;
p->next=s;
}
LNode *create(int *r, int n)
{
int j;
LNode *L,*head,*S;
if(n<=0)
return NULL;
head=new LNode;
S=new LNode;
L=head;
L->next=NULL;
for(j=2;j<n+2;j++)
insert(L,r[j-2],j);
return L;
}
void Delete(LNode *L,int i)
{
LNode *p,*aim;
p=search(L,i);
if(!p)
return;
aim=p->next;
p->next=aim->next;
delete aim;
}
void print(LNode *L)
{
LNode *read=L;
read=read->next;
while(read!=NULL)
{
cout<<read->data<<" ";
read=read->next;
}
cout<<endl;
}
int main()
{
//freopen("cin.txt","r",stdin);
int elem[20],i,n,head,end,run;
LNode *list;
cin>>n;
for(i=0;i<n;i++)
cin>>elem[i];
list=create(elem,n);
cin>>head>>end;
run=end-head+1;
while(run--)
Delete(list,head);
print(list);
return 0;
}
第三题:
Problem C: 岗位实践2-堆栈应用之数据加密
在古代有一种字符替换的加密技术:在输入一个字符串后,将它逆序加密一次,然后再顺序加密一次,得到最终密文。
用堆栈实现加密过程:建立两个堆栈A和B
1、把字符逐个压入堆栈A,逐个弹出并加密,然后压入堆栈B
2、把堆栈B的字符逐个弹出,并加密后输出,从而得到最终密文
密码表为: a-1 b-2 c-3 d-4 e-5 f-6 1-6 2-5 3-4 4-a 5-b 6-c
密码表中包含上述对应关系,就是把左边的字符转换为右边的字符。例如输入a,加密第1次把a转换为1,加密第2次把1转换为6,得到最终结果是6
必须使用堆栈数据结构来实现,有入栈、出栈、取栈顶等操作!!!
#include <iostream>
using namespace std;
#include <stack>
#include <string.h>
stack <char>A;
stack <char>B;
char trans(char a);
charora[12]={'a','b','c','d','e','f','1','2','3','4','5','6'};
charored[12]={'1','2','3','4','5','6','6','5','4','a','b','c'};
int main()
{
//freopen("cin.txt","r",stdin);
int t;
char temp[20];
cin>>t;
while(t--)
{
int size,i;
char ele;
cin>>temp;
size=strlen(temp);
for(i=0;i<size;i++)
A.push(temp[i]);
for(i=0;i<size;i++)
{
ele=A.top();
ele=trans(ele);
B.push(ele);
A.pop();
}
for(i=0;i<size;i++)
{
ele=B.top();
ele=trans(ele);
B.pop();
cout<<ele;
}
cout<<endl;
}
return 0;
}
char trans(char a)
{
char b;
int i;
for(i=0;i<12;i++)
if(a==ora[i])
b=ored[i];
return b;
}
第四题:
Problem D: 岗位实践2——队列应用之程序调度
在操作系统中往往需要对各种程序进行调度,一般根据任务优先级会分成几个任务队列,当优先级高的队列完成后才调度低优先级队列。当前设定有A B C三个任务队列,优先A高于B且B高于C。当前给出一个任务集合,请进行任务分类
#include <iostream>
using namespace std;
#include <queue>
int main()
{
//freopen("cin.txt","r",stdin);
int n,time[20],i;//i用于下标计数
cin>>n;
char client[20];
queue<int> client_kinds[3];
for(i=0;i<n;i++)
cin>>client[i];
for(i=0;i<n;i++)
{
cin>>time[i];
if(client[i]=='A')
client_kinds[0].push(time[i]);
else if(client[i]=='B')
client_kinds[1].push(time[i]);
else if(client[i]=='C')
client_kinds[2].push(time[i]);
}
int sum[3]={0,0,0},t[3]={0,0,0};
for(i=0;i<3;i++)
while(!client_kinds[i].empty())
{
sum[i]=client_kinds[i].front()+sum[i];
client_kinds[i].pop();
t[i]++;
}
for(i=0;i<t[0];i++)
cout<<"A ";
for(i=0;i<t[1];i++)
cout<<"B ";
for(i=0;i<t[2];i++)
cout<<"C ";
cout<<endl;
cout<<sum[0]/t[0]<<endl;
cout<<sum[1]/t[1]<<endl;
cout<<sum[2]/t[2]<<endl;
return 0;
}