职业导师网 http://www.51mentor.com
华为研发类笔试编程题
题目:网球中心共有 100 个网球场,每个单位可以来申请 1 到 100 的场地,申请的场地编号必须是连续的,
如果场地已经被其他单位占用,就不能再次使用,而且单位在使用完场地后必须归还。请设计一个完整的
系统(c 语言)。(限时 5 分钟)
Tennis.h
struct TennisGround
{
int num;
char *agentName;
};
typedef struct TennisGround TG;
void mallocTG(TG *total);
void freeTG(TG *total);
Tennis.c
#include
#include
#include
#include
#include "Tennis.h"
void mallocTG(TG *total)
{
int size, start,count = 0;
char *agentName = (char*)malloc(sizeof(char)*10);
printf("Please input your agentName:");
scanf("%s", agentName);
printf("Please input the size of the TennisGround:");
致力于打造中国最大、最专业和最具人性化的就业辅导和职场分享的互动平台。
scanf("%d", &size);
printf("Please input the TennisGround number you want to start:");
职业导师网 http://www.51mentor.com
scanf("%d", &start);
if((total+start)->agentName != " ")
{
printf("malloc failed!\n");
exit(-1);
}
else
{
while(count < size)
{
(total+start+count)->agentName = agentName;
count++;
}
}
}
void freeTG(TG* total)
{
char *an = (char*)malloc(sizeof(char)*10);
printf("please input agentName you want to free:");
scanf("%s", an);
int count = 0;
while(count < 100)
{
if(strcmp((total+count)->agentName, an) == 0)
(total+count)->agentName = " ";
count++;
}
}
int main()
{
int i;
int sw;
TG *total = (TG*)malloc(sizeof(TG)*100);
致力于打造中国最大、最专业和最具人性化的就业辅导和职场分享的互动平台。
职业导师网 http://www.51mentor.com
for(i=0; i<100; i++)
{
(total+i)->num = i;
(total+i)->agentName = " ";
}
while(1)
{
printf("*******Tennis Ground Mallocation******************\n");
for(i=0; i<100; i++)
{
printf("%d(%s) ", (total+i)->num, (total+i)->agentName);
if(i%5 == 0)
printf("\n");
}
printf("\n");
printf("**************************************************\n");
printf("Please input your choosen:(1-malloc,2-free):");
scanf("%d", &sw);
if(sw == 1)
mallocTG(total);
else
freeTG(total);
}
return 0;
}
致力于打造中国最大、最专业和最具人性化的就业辅导和职场分享的互动平台。