c语言模拟遥控小车

c语言模拟遥控小车,朋友给的竞赛题目

QQ图片20160623225739.jpg



#include "stdio.h"
struct point{
	int x;
	int y;
};
struct control{
	int t;//时间 
	char w;//方向 
};
int main(){
	struct point kaiche(int n,struct control opion[]);
	while(1){
	int n;
	scanf("%d",&n);	
	if(n==0)break;
	int i;
	struct control opion[n];
	struct point point={0,0 
	};
	for(i=0;i<n;i++){
		int d;char c;
		//d=30;c='R';
		scanf("%d %c",&d,&c);
		opion[i].t = d;
		opion[i].w = c;
	}
	point = kaiche(n,opion);
	printf("%d %d\n",point.x,point.y);
	}
	return 0;
}
struct point kaiche(int n,struct control opion[]){
	struct point n_point={0,0};
	//小车开始跑
	int nowcon=1;//当前方向 0=>x 1=>y 
	int zf = 1;//正负方向 
	int time=0;//时间 
	int lasttime=0;//上个状态的时间 
	int v=1;//速度 
	int isa=0;//1=>按下 是否按下了A 
	int i; 
	for(i=0;i<n;i++){
		int t = opion[i].t;
		time = t; 
		if(nowcon==1){
			//向y跑
			 n_point.y +=zf*(t-lasttime)*v;
		}else{
			//向x跑 
			n_point.x +=zf*(t-lasttime)*v;
		}
		lasttime = t;
		int w = opion[i].w;//方向
		if(w=='U'){nowcon=1;zf = 1;} 
		if(w=='D'){nowcon=1;zf = -1;}
		if(w=='L'){nowcon=0;zf = -1;}
		if(w=='R'){nowcon=0;zf = 1;} 
		if(w=='A'){nowcon=nowcon;zf = zf;
			if(isa==0){
				isa=1;
				v=2;
			}else{
				isa=0;
				v=1;
			}}
		if(n_point.y<0)n_point.y=0;
		if(n_point.x<0)n_point.x=0;
	}
	if(time<50){
		if(nowcon==1){
			//向y跑
			 n_point.y +=zf*(50-time)*v;
		}else{
			//向x跑 
			n_point.x +=zf*(50-time)*v;
		}
		if(n_point.y<0)n_point.y=0;
		if(n_point.x<0)n_point.x=0;
	}
	return n_point;
	//小车结束跑
}
转自 小草窝博客 。 
发表评论 / Comment

用心评论~