牛客网&&华为机试&&坐标移动
代码实现
#include<iostream>
#include<string>
#include<vector>
#include<cctype>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
string str;
int len1 = 0;
int len2 = 0;
int x = 0;
int y = 0;
int index1 = 0;
int index2 = 0;
//while(cin >> str) //循环输入数据
while (getline(cin, str))
{
len1 = str.size();
while (index2 != len1) //判断';'是否为最后一个字符串的下标,是的话就结束循环
{
if (str[index2] == ';') //字符直接用c1==c2就可以比较了,字符串用strcmp(c1,c2)来比较,等于0就是相等。
{
len2 = index2 - index1;
if((str[index1] == 'A') || (str[index1] == 'D') || (str[index1] == 'W') || (str[index1] == 'S')) //考虑优先级
{
if (len2 == 2) //一位字母一位数字
{
if (isdigit(str[index1 + 1]) && (index2 == index1 + 2))
{
switch(str[index1])
{
case 'A':x = x - (str[index1 + 1] - '0');break;
case 'D':x = x + (str[index1 + 1] - '0');break;
case 'W':y = y + (str[index1 + 1] - '0');break;
case 'S':y = y - (str[index1 + 1] - '0');break;
default: x = x ,y = y;break;
}
}
}
if (len2 == 3) //一位字母两位数字
{
if (isdigit(str[index1 + 1]) && isdigit(str[index1 + 2]) && (index2 == index1 + 3))
{
switch(str[index1])
{
case 'A':x = x - ((str[index1 + 1] - '0') * 10 + (str[index1 + 2] - '0'));break;
case 'D':x = x + ((str[index1 + 1] - '0') * 10 + (str[index1 + 2] - '0'));break;
case 'W':y = y + ((str[index1 + 1] - '0') * 10 + (str[index1 + 2] - '0'));break;
case 'S':y = y - ((str[index1 + 1] - '0') * 10 + (str[index1 + 2] - '0'));break;
default: x = x ,y = y;break;
}
}
}
}
index1 = index2 + 1;
}
index2 = index2 + 1;
}
cout << x << ',' << y << endl;
x = 0; //由于是循环输入,一些变量必须初始化
y = 0;
index1 = 0;
index2 = 0;
}
return 0;
}
编程笔记
- 代码实现解题思路:1.循环输入;2.下一层循环,判断’;‘是否为最后一个字符串的下标,是的话就结束循环;3.判断’;'中间的字符是否符合要求;4.一条字符串操作完毕,必须将一些变量初始化!!!为了测试下一条输入的字符串。
- 熟悉cctype字符库的使用。