【OJ中级】坐标移动

来源:互联网 发布:超玩fifaol3数据库 编辑:程序博客网 时间:2024/04/28 14:58

开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。

 

输入:

 

合法坐标为A(或者D或者W或者S) + 数字(两位以内)

坐标之间以;分隔。

非法坐标点需要进行丢弃。如AA10;  A1A;  $%$;  YAD; 等。

下面是一个简单的例子 如:

A10;S20;W10;D30;X;A1A;B10A11;;A10;

 

处理过程:

 

起点(0,0)

 

+   A10   =  (-10,0)

+   S20   =  (-10,-20)

+   W10  =  (-10,-10)

+   D30  =  (20,-10)

+   x    =  无效

+   A1A   =  无效

+   B10A11   =  无效

+  一个空 不影响

+   A10  =  (10,-10)

 

结果 (10, -10)

题目类别: 字符串 难度: 中级 运行时间限制:10Sec内存限制:128MByte阶段: 入职前练习 输入: 

一行字符串

 输出: 

最终坐标,以,分隔

 样例输入:
A10;S20;W10;D30;X;A1A;B10A11;;A10;                   
样例输出:
10,-10

流程:判断合法性——>1位数字或2位数字——>根据首位字母改变坐标X和Y


#include <iostream>#include <string>using  namespace std;int main( ){char strin[512];cin>>strin;int len=strlen(strin);int templen=0;int i=0,j=0,k=0;int x=0,y=0;int step=0;char temp[512];for(i=0;i<len;i++){//获取分号间内容if(';'!=strin[i])temp[j++]=strin[i];else{step=0;temp[j]='\0';templen=strlen(temp);//分类计算step值if(2==templen && '0'<=temp[1]&& '9'>=temp[1]){step=temp[1]-'0';}elseif(3==templen && ('0'<=temp[1]&& '9'>=temp[1])&&('0'<=temp[2]&& '9'>=temp[2]) ){step=(temp[1]-'0')*10+(temp[2]-'0');}elsestep=0;//坐标移动 switch(temp[0]){case 'A':x-=step;break;case 'D':x+=step;break;case 'S':y-=step;break;case 'W':y+=step;break;default:break;}j=0;memset(temp,0,512*sizeof(char));continue;}}cout<<x<<","<<y<<endl;return 0;}



0 0