题解 | #坐标移动#

坐标移动

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

#include<iostream>
#include <string>
#include<cstring>
#include<vector>
using namespace std;
 
int main() 
{
    char arr[100];//="A10;S20;W10;D30;X;A1A;B10A11;;A10;";		 //  A10;S20;W10;D30;X;A1A;B10A11;;A10;
    
    cin>>arr; 
    vector<string> vec;
    char  s[] = ";";
    char *ptr;
   	int x=0,y=0;
    /* 获取第一个子字符串 */
    ptr = strtok(arr, s);
    
    /* 继续获取其他的子字符串 */
    while( ptr != NULL ) 
    {
        //cout<<ptr<<endl;
        vec.push_back(ptr);
        ptr = strtok(NULL, s);
    }
//	for(int j=0;j<vec.size();j++)
//		cout<<vec[j]<<endl;
	for(vector<string>::iterator it=vec.begin();it!=vec.end();it++)
	{
		string str=*it;
		if(str.size()==3 && isupper(str[0])  && isdigit(str[1]) && isdigit(str[2]))
		{
			switch(str[0])
			{
				case 'A':  
					x = x-(10 *int(str[1]-'0')+(str[2]-'0'));
//					cout<<"(10 *(str[1]):"<<(10 *(str[1])<<endl;
					//cout<<"x:"<<x<<endl;
					break;
				case 'S':  
					y = y-(10 *int(str[1]-'0')+(str[2]-'0'));
//					cout<<"y:"<<x<<endl;
					break;
				case 'W':  
					y = y+(10 *int(str[1]-'0')+(str[2]-'0'));
//					cout<<"x:"<<x<<endl;
					break;
				case 'D':  
					x = x+(10 *int(str[1]-'0')+(str[2]-'0'));
//					cout<<"x:"<<x<<endl;
					break;			
			}
		}
		if(str.size()==2 && isupper(str[0])  && isdigit(str[1]))
		{
			switch(str[0])
			{
				case 'A':  
					x = x-int(str[1]-'0');
//					cout<<"x:"<<x<<endl;
					break;
				case 'S':  
					y = y-int(str[1]-'0');
//					cout<<"y:"<<x<<endl;
					break;
				case 'W':  
					y = y+int(str[1]-'0');
//					cout<<"x:"<<x<<endl;
					break;
				case 'D':  
					x = x+int(str[1]-'0');
//					cout<<"x:"<<x<<endl;
					break;			
			}
		}
		//cout<<str<<endl;
	}
	cout<<x<<","<<y<<endl;
		//cout<<*it<<endl;
    return 0;
}


全部评论

相关推荐

allin实习的大白...:我把第二个项目发出来了,如果感兴趣可以去研究研究,欢迎交流。 https://gitee.com/jtyjtyjty333/ind-dist-ai-sec-edge-cloud https://github.com/jtylab/ind-dist-ai-sec-edge-cloud
点赞 评论 收藏
分享
昨天 22:28
门头沟学院 Java
投递科大讯飞等公司6个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务