题解 | #坐标移动#

坐标移动

https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29

#include<stdio.h>
char* streffect(char *str_in);
int* strcoordinate(char* str_effect);
int main()
{
	//输入指令
	char str_in[10000] = { 0 };
	//printf("please input coordinate instruction:");
	gets(str_in);
	//printf("please insure your instruction:%s\n", str_in);

	//取出有效指令
	char* str_effect=streffect(str_in);

	//printf("str_effect:%s\n", str_effect);

	

	//输出坐标
	int* str_cooridinate=strcoordinate(str_effect);
	printf("%d,%d\n", str_cooridinate[0], str_cooridinate[1]);
	
}



#include<stdio.h>
char* streffect(char* str_in);
char* streffect(char* str_in)
{
	char str[10000] = { ';',0 };
	//创造指令有效性判断条件的数组
	for (size_t io = 0; str_in[io] != 0; io++)
	{
		str[io + 1] = str_in[io];
	}
	//printf("STR:%s\n", str);

	//指令解析,外层str循环取;间指令,内层str_effect循环——记录有效指令
	char str_effect[10000] = { 0 };
	size_t e = 0;
	size_t i = 0, j, k;

	if (str[i] == ';')
	{
		for (j = i + 1; str[j] != 0; j++)
		{
			if (str[j] == ';')
			{
				switch (j - i)
				{
				case 3:
					if (str[i + 1] == 'A' || str[i + 1] == 'W' || str[i + 1] == 'S' || str[i + 1] == 'D')
					{
						if (str[i + 2] >= 48 && str[i + 2] <= 57)
						{

							str_effect[e] = str[i + 1];
							str_effect[e + 1] = 'O';//'O'=79
							str_effect[e + 2] = str[i + 2];
							
							e += 3;//下一组指令

						}
					}
					
					break;
				case 4:// ;A10;
					if (str[i + 1] == 'A' || str[i + 1] == 'W' || str[i + 1] == 'S' || str[i + 1] == 'D')
					{
						if (str[i + 2] >= 48 && str[i + 2] <= 57)
						{
							if (str[i + 3] >= 48 && str[i + 3] <= 57)
							{

								for (k = 0; k < 3; k++)//取有效指令给str_efect
								{
									str_effect[e + k] = str[i + k + 1];
								}
								e += 3;//下一组指令

							}
						}

					}

					break;
				default:
					break;
				}
				i = j;//重置分号位置下标
			}
		}
	}

	return str_effect;

}
#include<stdio.h>

int* strcoordinate(char* str_effect)
{
	int str_coordinate[2] = { 0,0 };

	for (size_t i = 0; str_effect[3*i+2] != 0; i++)
	{
		char a = str_effect[3 * i];
		int b = str_effect[3 * i + 1] - 48;
		int c = str_effect[3 * i + 2] - 48;
		if (b == 'O' - 48)
		{
			b = '0'-48;
		}
		switch (a)
		{
		case 'A':
			str_coordinate[0] = str_coordinate[0] - (b * 10 + c);
			break;


		case 'D':
			str_coordinate[0] = str_coordinate[0] + (b * 10 + c);
			break;

		case 'S':
			str_coordinate[1] = str_coordinate[1] - (b * 10 + c);
			break;

		case 'W':
			str_coordinate[1] = str_coordinate[1] +(b * 10 + c);
			break;

		default:
			break;
		}


	}



	return str_coordinate;

}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务