malloc函数的使用
#include<stdio.h>
#include<malloc.h>
int main()
{
int *p,*q;
malloc(4);//进行定义
// (int *)malloc(sizeof(int)) 傻瓜式定义
p=(int *)malloc(sizeof(int));
q=p;//进行一个保存
printf("%d",*p);
p++;//像数组一样的连续使用
free(q);//内存释放
}
题目E-E(以为很简单,使用字符函数和队列就能做出来。可能是我水平太低,认为题目很简单吧)
代码运行没有错误,但是刷题网站判定依然是答案错误,还在想为什么。
#include<iostream>
#include<string.h>
using namespace std;
const int max_n=1e4+10;
int Q[max_n];
int front,back;
void push(int x)
{
Q[back++]=x;
}
int pop()
{
return Q[front++];
}
//每组数据后有一个空行。
int main()
{
char s[100];
int T;
while(scanf("%d",&T)!=EOF)
{
while(T--)
{
//gets(s);
scanf("%s",s);
//enqueue x,将元素x插入队尾
if(strcmp(s,"enqueue")==0)//进行比较
{
int x;
scanf("%d",&x);
push(x);
}
//equeue,若队列非空,则删去队头元素,并输出该元素
//每次执行dequeue操作时删除队头元素输出并换行,如果队列为空输出“this is empty!”并换行。
if(strcmp(s,"dequeue")==0)
{
if(front==back)
{
printf("this is empty!\n");
}
else
{
printf("%d\n",Q[front]);
pop();
}
}
//query,从队头开始删除所有元素,并输出。
//每次执行query操作时删除所有元素队列内所有元素并输出,每个元素占一行
//如果栈为空输出“this is empty!”并换行。
if(strcmp(s,"query")==0)
{
if(front==back)
{
printf("this is empty!\n");
}
else
{
while(front!=back)
{
printf("%d\n",Q[front]);
pop();
}
}
}
}
}
}