nefu瑞格栈与递归、队列
–题目来源-nefu瑞格
栈与递归
4227
#include<stdio.h>
// write your code here
int sum(int a[],int n)
{
if(n==1)
return a[0];
else
return sum(a,n-1)+a[n-1];
}
int main()
{
int a[6],i;
for (i=0;i<6;i++)
scanf("%d", a+i);
printf("%d\n", sum(a, 6));
return 0;
}
4226
#include<stdio.h>
// write your code here
int max(int a[],int n)
{
if(n==2)
{
if(a[0]>a[1])
return a[0];
else
return a[1];
}
else
{
if(max(a,n-1)>a[n-1])
return max(a,n-1);
else
return a[n-1];
}
}
int main()
{
int a[6],i;
for (i=0;i<6;i++)
scanf("%d", a+i);
printf("%d\n", max(a, 6));
return 0;
}
4228
#include <stdio.h>
// write your code here
float average(float a[],int n)
{
if(n==1)
return a[0];
else
return (average(a,n-1)*(n-1)+a[n-1])/n;
}
int main()
{
float a[6];
int i;
for (i=0;i<6;i++)
scanf("%f", a+i);
printf("%.2f\n", average(a, 6));
return 0;
}
队列
4236
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef struct Node {
int data;
struct Node *next;
}Node, *pNode;
pNode p;
void InitQueue()
{
p = (struct Node *)malloc(sizeof(struct Node));
p->next = p;
}
void EnQueue(const int x)
{
// write your code here
pNode r,t=p;
r = (struct Node *)malloc(sizeof(struct Node));
r->data=x;
while(t->next!=p)
{
t=t->next;
}
t->next=r;
r->next=p;
}
const Status isEmpty()
{
// write your code here
if(p->next==p)
return 1;
else
return 0;
}
Status DeQueue(int *x)
{
// write your code here
pNode r=p,t=p;
while(r->next!=p)
r=r->next;
*x=p->data;
p=p->next;
free(t);
r->next=p;
}
const void Print()
{
Node *t = p->next;
while (t->next!=p->next) {
printf("%d ", t->data);
t = t->next;
}
printf("\n");
}
int main()
{
InitQueue();
int x;
EnQueue(1);
EnQueue(2);
EnQueue(3);
DeQueue(&x);
EnQueue(4);
DeQueue(&x);
DeQueue(&x);
DeQueue(&x);
EnQueue(5);
Print();
return 0;
}
4915
#include<iostream>
using namespace std;
struct Node {
int data;
Node *next;
};
class LinkList {
private:
Node *first;
public:
LinkList();
void Insert(const int &x);
Node *First();
int Length(Node *n);
};
LinkList::LinkList()
{
first = new Node();
first->next = NULL;
}
void LinkList::Insert(const int &x)
{
Node *t = first;
while (t->next!=NULL)
t = t->next;
Node *n = new Node();
n->data = x;
n->next = t->next;
t->next = n;
}
Node *LinkList::First()
{
return first;
}
//write your code here
int LinkList::Length(Node *n)
{
if(n==NULL)
return 0;
else
return this->Length(n->next)+1;
}
int main()
{
LinkList l;
int x;
cin >> x;
while (x!=-1) {
l.Insert(x);
cin >> x;
}
//write your code here
Node *t;
t=l.First();
printf("%d",l.Length(t->next));
return 0;
}
4907
B
4872
#include<stdio.h>
#include<stdlib.h>
typedef struct QNode
{
int data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
int InitQueue(LinkQueue &Q)
{
Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
if( !Q.front) exit(0);
Q.front->next = NULL;
return 1;
}
int EnQueue(LinkQueue &Q, int e)
{
QueuePtr p;
p=new QNode;
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
}
int DeQueue(LinkQueue &Q,int &e)
{
QueuePtr p;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
delete p;
}
int main()
{
int m,n;
int i,e;
LinkQueue Q;
InitQueue(Q);
scanf("%d",&n);
for(i = 0 ; i < n ; ++i)
{
scanf("%d",&e);
EnQueue(Q,e);
}
scanf("%d",&m);
for(i = 0 ; i < m ; ++i)
{
DeQueue(Q,e);
printf("%d ",e);
}
printf("\n");
return 0;
}
4219
A
4220
B
4235
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define TRUE 1
#define FALSE 0
typedef int Status;
int *q;
int maxSize;
int front;
int rear;
int tag;
const Status isFull()
{
// write your code here
if(front==rear&&tag==1)
return 1;
else
return 0;
}
const Status isEmpty()
{
// write your code here
if(front==rear&&tag==0)
return 1;
else
return 0;
}
void InitStack(int sz)
{
maxSize = sz;
q = (int *)malloc(maxSize*sizeof(int));
front = 0;
rear = 0;
tag = 0;
}
void output()
{
int i;
for (i=0;i<maxSize;i++)
printf("%d ", q[i]);
}
Status EnQueue(const int x)
{
// write your code here
if(isFull())
return 0;
else
{
q[rear]=x;
rear=(rear+1)%maxSize;
if(rear==front)
tag=1;
return 1;
}
}
Status DeQueue(int x)
{
// write your code here
if(isEmpty())
return 0;
else
{
x=q[front];
q[front]=0;
front=(front+1)%maxSize;
if(rear==front)
tag=0;
return 1;
}
}
int main()
{
InitStack(4);
int m;
EnQueue(1);
DeQueue(m);
DeQueue(m);
EnQueue(2);
EnQueue(3);
EnQueue(4);
EnQueue(5);
DeQueue(m);
DeQueue(m);
EnQueue(6);
output();
return 0;
}