题解 | #坐标移动#

坐标移动

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;

}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
11086次浏览 95人参与
# 你的实习产出是真实的还是包装的? #
1960次浏览 42人参与
# MiniMax求职进展汇总 #
24121次浏览 309人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7643次浏览 43人参与
# 简历第一个项目做什么 #
31749次浏览 341人参与
# 重来一次,我还会选择这个专业吗 #
433551次浏览 3926人参与
# 米连集团26产品管培生项目 #
6040次浏览 216人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187211次浏览 1122人参与
# 牛客AI文生图 #
21447次浏览 238人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152456次浏览 888人参与
# 研究所笔面经互助 #
118967次浏览 577人参与
# 简历中的项目经历要怎么写? #
310370次浏览 4219人参与
# AI时代,哪些岗位最容易被淘汰 #
63836次浏览 828人参与
# 面试紧张时你会有什么表现? #
30513次浏览 188人参与
# 你今年的平均薪资是多少? #
213146次浏览 1039人参与
# 你怎么看待AI面试 #
180146次浏览 1258人参与
# 高学历就一定能找到好工作吗? #
64331次浏览 620人参与
# 你最满意的offer薪资是哪家公司? #
76546次浏览 374人参与
# 我的求职精神状态 #
448136次浏览 3129人参与
# 正在春招的你,也参与了去年秋招吗? #
363523次浏览 2638人参与
# 腾讯音乐求职进展汇总 #
160677次浏览 1112人参与
# 校招笔试 #
471210次浏览 2964人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务