#include<stdio.h>
#include<string.h>
#include<math.h>
#include<malloc.h>
#define speed_h 10
#define north 0
#define west 1
#define south 2
#define east 3
//有一辆智能小车,最初(时间为0)的位置为(0,0),我们想知道它最后的位置。
//小车以每小时10公里的速度向北移动(以北为y轴正向,以东为x轴正向)。
//小车会受到一系列依照时间戳记排序的命令,1表示“向左转”,2表示“向右转”,3表“停止”。
//每个命令的前面有一个时间戳记,所以我们知道该命令是何时发出的。
//最后一个命令一定是“停止”。我们另外假设,这辆小车非常灵活,它可以在瞬间转弯。
//以下列输入为例。小车在时间为5的时候收到一个“向左转”的命令1,在时间10收到一个“向右转”的命令2,
//在时间15收到一个“停止”的命令3。那么在最后时间15的时候,小车的位置将在(-50,100)。
//程序只要求输出小车最后的位置,第一个整数是x坐标,第二个整数是y坐标。
//输入包含多个命令,每个命令由整数time和command组成,表示在时刻time发出命令command。command的取值范围1-3,含义如上所述。
//输出占一行,包含两个整数,表示小车的最终位置。两个整数之间由空格隔开。
//此题信息量大,考查内容较为综合。
//(1)数字化。可为四个方向编号,自向北开始,逆时针将4个方向依次编号为0,1,2,3。
//当接到向左转命令,方向号增1,向右转则方向号减1。为避免出现负数或大于3的情况,可对4取模。
int main()
{
//思路:开始的方向已经确定,第一个转弯要么向左要么向右,先左转就按逆时针,右转按顺时针
//先给出时间发出3就停止
int time=0,command=0;//time随意取,command取1-3
int x=0,y=0;
int count=0;
int flag=0;
scanf("%d",&time);//开头固定向北走一段时间
y+=speed_h*time;
int timepre=0;
timepre=time;
int time_gap=0;
int cnt=0;
int num=0;
while(1)
{
scanf("%d",&command);//给出第一个命令
if(command==3)
break;
scanf("%d",&time);//一个命令持续时间
time_gap=time-timepre;
timepre=time;
//第4次换思路
//1,2,1/2 ok逆时针北西南东,顺时针北东南西
if(command==1)
{
cnt++;
if(cnt>=0)
{
num=abs(cnt)%4;
if(num==north)
y+=speed_h* time_gap;//北
else if(num==west)
x+=-speed_h* time_gap;//西
else if(num==south)
y+=-speed_h* time_gap;//南
else if(num==east)
x+=speed_h* time_gap;//东
}else
{
num=abs(cnt)%4;
// if(num==0)
// y+=speed_h* time_gap;//北
// else
if(num==3)
x+=-speed_h* time_gap;//西
else if(num==2)
y+=-speed_h* time_gap;//南
else if(num==1)
x+=speed_h* time_gap;//东
}
}else if(command==2)
{
cnt--;
if(cnt>0)
{
num=abs(cnt)%4;
// if(num==0)
// y+=speed_h* time_gap;//北
// else
if(num==1)
x+=-speed_h* time_gap;//西
else if(num==2)
y+=-speed_h* time_gap;//南
else if(num==3)
x+=speed_h* time_gap;//东
}
else
{
num=abs(cnt)%4;
if(num==0)
y+=speed_h* time_gap;//北
else if(num==3)
x+=-speed_h* time_gap;//西
else if(num==2)
y+=-speed_h* time_gap;//南
else if(num==1)
x+=speed_h* time_gap;//东
}
}
// if(flag==1) //逆时针
// {
// if(command==1)
// {
// cnt++;
// if(num==north)
// y+=speed_h* time_gap;//北
// else if(num==west)
// x+=-speed_h* time_gap;//西
// else if(num==south)
// y+=-speed_h* time_gap;//南
// else if(num==east)
// x+=speed_h* time_gap;//东
// }else if(command==2)
// {
// cnt--;
// if(num==north)
// y+=speed_h* time_gap;//北
// else if(num==west)
// x+=-speed_h* time_gap;//西
// else if(num==south)
// y+=-speed_h* time_gap;//南
// else if(num==east)
// x+=speed_h* time_gap;//东
// }
// }
// if(flag==2)//顺时针
// {
// if(command==2)
// {
// cnt--;
// if(num==north)
// y+=speed_h* time_gap;//北
// else if(num==west)
// x+=speed_h* time_gap;//东
// else if(num==south)
// y+=-speed_h* time_gap;//南
// else if(num==east)
// x+=-speed_h* time_gap;//西
// }
// else if(command==1)
// {
// cnt++;
// if(num==north)
// y+=speed_h* time_gap;//北
// else if(num==west)
// x+=speed_h* time_gap;//西
// else if(num==south)
// y+=-speed_h* time_gap;//南
// else if(num==east)
// x+=-speed_h* time_gap;//东
// }
// }
// scanf("%d",&command);//给出第一个命令
// switch(command)
// {
// case(1):
// {
// cnt++;
// if(abs(cnt)%4==north)
// y+=speed_h* time_gap;//北
// if((abs(cnt)%4)==west)
// x+=-speed_h* time_gap;//西
// if(abs(cnt)%4==south)
// y+=-speed_h* time_gap;//南
// if(abs(cnt)%4==west)
// x+=speed_h* time_gap;//东
// }
// break;
//
// case(2):
// {
// cnt--;
// if(abs(cnt)%4==north)
// y+=speed_h* time_gap;
// if(abs(cnt)%4==west)
// x+=-speed_h* time_gap;
// if(abs(cnt)%4==south)
// y+=-speed_h* time_gap;
// if(abs(cnt)%4==west)
// x+=speed_h* time_gap;
// }
// break;
// default:break;
// }
// if(command==1)//第一次左转
// {
// cnt++;
// switch(abs(cnt))
// {
// case(1):x+=-speed_h* time_gap;break;
// case(2):y+=-speed_h* time_gap;break;
// case(3):x+=speed_h* time_gap;break;
// case(4):y+=speed_h* time_gap;break;
// default:break;
// }
//
//
// }
// if(command==2)
// {
// cnt--;
// switch(abs(cnt))
// {
// case(1):x+=speed_h* time_gap;break;
// case(2):y+=-speed_h* time_gap;break;
// case(3):x+=-speed_h* time_gap;break;
// case(4):y+=speed_h* time_gap;break;
// default:break;
// }
//
//
// }
//
// if(abs(cnt)%4==0)
// cnt==0;
// if(count==0)//每四次选择方向达成一个周期
// {
// switch(command)
// {
// case(1):x+=-speed_h* time_gap;break;//x负半轴方向
// case(2):x+=speed_h* time_gap;break;//x正半轴方向
// default:break;
// }
// }
//
// else if(count==1)
// {
// switch(command)
// {
// case(1):y+=-speed_h* time_gap;break;//y负半轴方向
// case(2):y+=speed_h* time_gap;break;//y正半轴方向
// default:break;
// }
// }
//
// else if(count==2)
// {
// switch(command)
// {
// case(1):x+=speed_h* time_gap;break;//x正半轴方向
// case(2):x+=-speed_h* time_gap;break;//x负半轴方向
// default:break;
// }
// }
// else if(count==3)
// {
// switch(command)
// {
// case(1):y+=speed_h* time_gap;break;//y正半轴方向
// case(2):y+=-speed_h* time_gap;break;//y负半轴方向
// default:break;
// }
// }
// count++;
//
// if(count==4)
// count=0;//一个循环已经结束了,记方向的标志置0
}
printf("%d %d",x,y);
return 0;//今天又是没带return 0的一天
}
誒,我是废物啊,写了一上午,写出来了,下面这个是对的!!!!可以根据我前面的代码找找自己错在那里了,我是废物,呜呜呜@...__@...
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<malloc.h>
#define speed_h 10
int main()
{
//思路:开始的方向已经确定,第一个转弯要么向左要么向右,先左转就按逆时针,右转按顺时针
//先给出时间发出3就停止
int time=0,command=0;//time随意取,command取1-3
int x=0,y=0;
int flag=0;
scanf("%d",&time);//开头固定向北走一段时间
y+=speed_h*time;
int timepre=0;
timepre=time;
int time_gap=0;
int cnt=0;
int num=0;
while(1)
{
scanf("%d",&command);//给出第一个命令
if(command==3)
break;
scanf("%d",&time);//一个命令持续时间
time_gap=time-timepre;
timepre=time;
//第6次换思路
//1,2,1/2 ok逆时针北西南东,顺时针北东南西
if(command==1)
{
cnt++;
if(cnt>=0)
{
num=abs(cnt)%4;
if(num==0)
y+=speed_h* time_gap;//北
else if(num==1)
x+=-speed_h* time_gap;//西
else if(num==2)
y+=-speed_h* time_gap;//南
else if(num==3)
x+=speed_h* time_gap;//东
}else
{
num=abs(cnt)%4;
if(num==0)
y+=speed_h* time_gap;//北
else
if(num==3)
x+=-speed_h* time_gap;//西
else if(num==2)
y+=-speed_h* time_gap;//南
else if(num==1)
x+=speed_h* time_gap;//东
}
}else if(command==2)
{
cnt--;
if(cnt>=0)
{
num=abs(cnt)%4;
if(num==0)
y+=speed_h* time_gap;//北
else
if(num==1)
x+=-speed_h* time_gap;//西
else if(num==2)
y+=-speed_h* time_gap;//南
else if(num==3)
x+=speed_h* time_gap;//东
}
else
{
num=abs(cnt)%4;
if(num==0)
y+=speed_h* time_gap;//北
else
if(num==3)
x+=-speed_h* time_gap;//西
else if(num==2)
y+=-speed_h* time_gap;//南
else if(num==1)
x+=speed_h* time_gap;//东
}
}
}
printf("%d %d",x,y);
return 0;//今天又是没带return 0的一天
}