c语言编程习题求解
有一辆卡车行驶在沙漠中,我们想知道它最后的位置。卡车最初(时间为0)是在位置(0,0)以每小时10公里的速度向北移动。卡车会收到一系列依照时间戳记排序的命令,1表示「向左...
有一辆卡车行驶在沙漠中,我们想知道它最后的位置。卡车最初 (时间为 0) 是在位置 (0, 0) 以每小时 10 公里的速度向北移动。卡车会收到一系列依照时间戳记排序的命令,1 表示「向左转」,2 表示「向右转」,3 表示「停止」。每个命令的前面有一个时间戳记,所以我们知道该命令是何时发出的。最后一个命令一定是「停止」。我们另外还假设,这辆卡车非常灵活,所以它可以在瞬间转弯。 以下列输入为例。卡车在时间为 5 的时候收到一个「向左转」的命令 1,在时间 10 收到一个「向右转」的命令 2,在时间 15 收到一个「停止」的命令 3。那么最后在时间 15 的时候,卡车的位置将在 (-50, 100)。程式只需要输出卡车的最后位置。第一个数字是 x 坐标,第二个数字是 y 坐标。
输入范例
5
1
10
2
15
3
输出范例
-50 100 展开
输入范例
5
1
10
2
15
3
输出范例
-50 100 展开
3个回答
展开全部
so easy.但是时间不够我回答了。
#include <stdio.h>
#define CAR_SPEED 10
typedef enum _direction
{
EAST = 0,
WEST,
SOUTH,
NORTH,
CENTER,
} eDirection;
typedef enum _turnType
{
TURN_LEFT = 1,
TURN_RTIGHT = 2,
TURN_STOP = 3,
} eTurnType_t;
typedef struct _position
{
int x;
int y;
} Position_t;
typedef struct _CtrlCmd
{
unsigned int time;
eTurnType_t turn;
} CtrlCmd_t;
CtrlCmd_t CtrlCmdSeq[] = {{5, 1}, {10, 2}, {15, 3}};
const char *direStr[4] = {"东", "西", "南", "北"};
int main(void)
{
int i;
int ctrlCmdNum;
unsigned int lastTime;
Position_t nowPlace;
eDirection lastDire;
lastTime = 0;
nowPlace.x = 0;
nowPlace.y = 0;
lastDire = NORTH;
printf("卡车从X=%d Y=%d点出发,正驶向%s方\n\r", nowPlace.x, nowPlace.y, direStr[lastDire]);
ctrlCmdNum = sizeof(CtrlCmdSeq) / sizeof(CtrlCmdSeq[0]);
for (i=0; i<ctrlCmdNum; ++i)
{
if (lastTime > CtrlCmdSeq[i].time)
{
printf("卡车控制命令序列时间有误,你这是想控制时光倒流么,亲\n\r");
return 0;
}
switch (lastDire)
{
case NORTH:
nowPlace.y += CAR_SPEED * (CtrlCmdSeq[i].time - lastTime);
if (TURN_LEFT == CtrlCmdSeq[i].turn)
lastDire = WEST;
else if (TURN_RTIGHT == CtrlCmdSeq[i].turn)
lastDire = EAST;
else if (TURN_STOP == CtrlCmdSeq[i].turn)
lastDire = CENTER;
break;
case SOUTH:
nowPlace.y -= CAR_SPEED * (CtrlCmdSeq[i].time - lastTime);
if (TURN_LEFT == CtrlCmdSeq[i].turn)
lastDire = EAST;
else if (TURN_RTIGHT == CtrlCmdSeq[i].turn)
lastDire = WEST;
else if (TURN_STOP == CtrlCmdSeq[i].turn)
lastDire = CENTER;
break;
case EAST:
nowPlace.x += CAR_SPEED * (CtrlCmdSeq[i].time - lastTime);
if (TURN_LEFT == CtrlCmdSeq[i].turn)
lastDire = NORTH;
else if (TURN_RTIGHT == CtrlCmdSeq[i].turn)
lastDire = SOUTH;
else if (TURN_STOP == CtrlCmdSeq[i].turn)
lastDire = CENTER;
break;
case WEST:
nowPlace.x -= CAR_SPEED * (CtrlCmdSeq[i].time - lastTime);
if (TURN_LEFT == CtrlCmdSeq[i].turn)
lastDire = SOUTH;
else if (TURN_RTIGHT == CtrlCmdSeq[i].turn)
lastDire = NORTH;
else if (TURN_STOP == CtrlCmdSeq[i].turn)
lastDire = CENTER;
break;
default :
printf("控制卡车方向失灵,难道你想让卡车钻到路下或腾跳起来么,亲\n\r");
return 0;
//break;
}
if (CENTER == lastDire)
{
printf("卡车已经到达目的地X=%d, Y=%d。\n\r", nowPlace.x, nowPlace.y);
return 0;
}
lastTime = CtrlCmdSeq[i].time;
printf("卡车到达X=%d Y=%d,驶向%s方\n\r", nowPlace.x, nowPlace.y, direStr[lastDire]);
}
return 0;
}
#include <stdio.h>
#define CAR_SPEED 10
typedef enum _direction
{
EAST = 0,
WEST,
SOUTH,
NORTH,
CENTER,
} eDirection;
typedef enum _turnType
{
TURN_LEFT = 1,
TURN_RTIGHT = 2,
TURN_STOP = 3,
} eTurnType_t;
typedef struct _position
{
int x;
int y;
} Position_t;
typedef struct _CtrlCmd
{
unsigned int time;
eTurnType_t turn;
} CtrlCmd_t;
CtrlCmd_t CtrlCmdSeq[] = {{5, 1}, {10, 2}, {15, 3}};
const char *direStr[4] = {"东", "西", "南", "北"};
int main(void)
{
int i;
int ctrlCmdNum;
unsigned int lastTime;
Position_t nowPlace;
eDirection lastDire;
lastTime = 0;
nowPlace.x = 0;
nowPlace.y = 0;
lastDire = NORTH;
printf("卡车从X=%d Y=%d点出发,正驶向%s方\n\r", nowPlace.x, nowPlace.y, direStr[lastDire]);
ctrlCmdNum = sizeof(CtrlCmdSeq) / sizeof(CtrlCmdSeq[0]);
for (i=0; i<ctrlCmdNum; ++i)
{
if (lastTime > CtrlCmdSeq[i].time)
{
printf("卡车控制命令序列时间有误,你这是想控制时光倒流么,亲\n\r");
return 0;
}
switch (lastDire)
{
case NORTH:
nowPlace.y += CAR_SPEED * (CtrlCmdSeq[i].time - lastTime);
if (TURN_LEFT == CtrlCmdSeq[i].turn)
lastDire = WEST;
else if (TURN_RTIGHT == CtrlCmdSeq[i].turn)
lastDire = EAST;
else if (TURN_STOP == CtrlCmdSeq[i].turn)
lastDire = CENTER;
break;
case SOUTH:
nowPlace.y -= CAR_SPEED * (CtrlCmdSeq[i].time - lastTime);
if (TURN_LEFT == CtrlCmdSeq[i].turn)
lastDire = EAST;
else if (TURN_RTIGHT == CtrlCmdSeq[i].turn)
lastDire = WEST;
else if (TURN_STOP == CtrlCmdSeq[i].turn)
lastDire = CENTER;
break;
case EAST:
nowPlace.x += CAR_SPEED * (CtrlCmdSeq[i].time - lastTime);
if (TURN_LEFT == CtrlCmdSeq[i].turn)
lastDire = NORTH;
else if (TURN_RTIGHT == CtrlCmdSeq[i].turn)
lastDire = SOUTH;
else if (TURN_STOP == CtrlCmdSeq[i].turn)
lastDire = CENTER;
break;
case WEST:
nowPlace.x -= CAR_SPEED * (CtrlCmdSeq[i].time - lastTime);
if (TURN_LEFT == CtrlCmdSeq[i].turn)
lastDire = SOUTH;
else if (TURN_RTIGHT == CtrlCmdSeq[i].turn)
lastDire = NORTH;
else if (TURN_STOP == CtrlCmdSeq[i].turn)
lastDire = CENTER;
break;
default :
printf("控制卡车方向失灵,难道你想让卡车钻到路下或腾跳起来么,亲\n\r");
return 0;
//break;
}
if (CENTER == lastDire)
{
printf("卡车已经到达目的地X=%d, Y=%d。\n\r", nowPlace.x, nowPlace.y);
return 0;
}
lastTime = CtrlCmdSeq[i].time;
printf("卡车到达X=%d Y=%d,驶向%s方\n\r", nowPlace.x, nowPlace.y, direStr[lastDire]);
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
int main()
{
int time_front,time_behind,order;//时间前节点、时间后节点、转向命令
int x,y;//卡车位置坐标
int flag;//卡车当前方向 : 1-向北 2-向西 3-向南 4-向东
x = y = 0 ;
flag = 1 ;
time_front = 0;
scanf("%d%d",&time_behind,&order);
while(1)
{
switch(flag)
{
case 1:y += 10 * ( time_behind - time_front );
flag = (order==1)?2:4; time_front = time_behind;break;
case 2:x -= 10 * ( time_behind - time_front );
flag = (order==1)?3:1; time_front = time_behind;break;
case 3:y -= 10 * ( time_behind - time_front );
flag = (order==1)?4:2; time_front = time_behind;break;
case 4:x += 10 * ( time_behind - time_front );
flag = (order==1)?1:3; time_front = time_behind;break;
default:break;
}
if(order == 3) break;;
scanf("%d%d",&time_behind,&order);
}
printf("%d,%d\n",x,y);
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询