C语言计算小汽车的位置

题目描述

有一辆智能小车,最初(时间为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,含义如上所述。
输出
输出占一行,包含两个整数,表示小车的最终位置。两个整数之间由空格隔开。
样例输入 Copy
5
1
10
2
15
3
样例输出 Copy
-50 100

#include<stdio.h>
int main(){
	int time,command,x=0,y=0;
	int time2=0,direction=0,t;
	while(scanf("%d\n%d",&time,&command)){
		switch(direction){//计算小车的位置
			case 0:y=y+(time-time2)*10;break;
			case 1:x=x-(time-time2)*10;break;
			case 2:y=y-(time-time2)*10;break;
			case 3:x=x+(time-time2)*10;break;
		}
		if(command==3)//停止条件
		  break;
		else if(command==1)//
		  direction++;//行驶命令
		else//
		  direction--;//
		direction=(direction+4)%4;//方向为四个一循环,同时也避免出现负数
	time2=time;
}
	printf("%d %d",x,y);
	return 0;
}
道阻且长,行则将至,行而不辍,未来可期。
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
实现汽车牌照的快速查找系统可以使用哈希表进行实现,具体步骤如下: 1. 定义哈希表结构体,包含哈希表的大小,以及哈希表数组的指针。 2. 定义车辆信息结构体,包含车牌号码、车主姓名、车辆型号等信息。 3. 定义哈希函数,将车牌号码转换为哈希表的下标。 4. 实现哈希表的插入操作,将车辆信息插入到哈希表中。 5. 实现哈希表的查找操作,根据车牌号码查找对应的车辆信息。 代码实现如下: ```c #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_SIZE 10000 typedef struct CarInfo { char licensePlate[10]; char ownerName[20]; char carModel[20]; } CarInfo; typedef struct HashTable { int size; CarInfo **table; } HashTable; int hash(char *licensePlate) { int sum = 0; for (int i = 0; i < strlen(licensePlate); i++) { sum += licensePlate[i]; } return sum % MAX_SIZE; } HashTable *createHashTable(int size) { HashTable *hashTable = (HashTable *) malloc(sizeof(HashTable)); hashTable->size = size; hashTable->table = (CarInfo **) malloc(sizeof(CarInfo *) * size); for (int i = 0; i < size; i++) { hashTable->table[i] = NULL; } return hashTable; } void insert(HashTable *hashTable, CarInfo *carInfo) { int index = hash(carInfo->licensePlate); while (hashTable->table[index] != NULL) { index++; index %= MAX_SIZE; } hashTable->table[index] = carInfo; } CarInfo *find(HashTable *hashTable, char *licensePlate) { int index = hash(licensePlate); while (hashTable->table[index] != NULL && strcmp(hashTable->table[index]->licensePlate, licensePlate) != 0) { index++; index %= MAX_SIZE; } if (hashTable->table[index] != NULL) { return hashTable->table[index]; } else { return NULL; } } int main() { HashTable *hashTable = createHashTable(MAX_SIZE); CarInfo car1 = {"京A12345", "张三", "宝马"}; CarInfo car2 = {"京B67890", "李四", "奔驰"}; CarInfo car3 = {"京C24680", "王五", "奥迪"}; insert(hashTable, &car1); insert(hashTable, &car2); insert(hashTable, &car3); char licensePlate[10] = "京B67890"; CarInfo *carInfo = find(hashTable, licensePlate); if (carInfo != NULL) { printf("车牌号码:%s\n", carInfo->licensePlate); printf("车主姓名:%s\n", carInfo->ownerName); printf("车辆型号:%s\n", carInfo->carModel); } else { printf("未找到车辆信息\n"); } return 0; } ``` 在上述代码中,我们使用哈希函数将车牌号码转换为哈希表的下标,然后使用线性探测的方法解决哈希冲突,将车辆信息插入到哈希表中。在查找车辆信息时,我们根据车牌号码计算哈希值,然后找到对应的哈希表下标,如果该位置有车辆信息且车牌号码与待查找的车牌号码相同,则返回该车辆信息。否则,继续线性探测。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱码怪才

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值