题目
开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。
输入:
合法坐标为A(或者D或者W或者S) + 数字(两位以内)
坐标之间以;分隔。
非法坐标点需要进行丢弃。如AA10; A1A; <nobr><span class="math" id="MathJax-Span-1" style="width: 0.003em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.003em; height: 0px; font-size: 125%;"><span style="position: absolute; clip: rect(3.843em 1000.003em 4.163em -0.477em); top: -3.997em; left: 0.003em;"><span class="mrow" id="MathJax-Span-2"></span><span style="display: inline-block; width: 0px; height: 4.003em;"></span></span></span><span style="border-left-width: 0.003em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 0.137em; vertical-align: -0.063em;"></span></span></nobr><script type="math/tex" id="MathJax-Element-1">%</script>; 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
代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void PointMove(string str,int &x,int &y){
int size = str.size();
if(size == 0){
return;
}
vector<string> vec;
int start = 0,end = 0;
while(end != -1){
end = str.find(";",start);
vec.push_back(str.substr(start,end-start));
start = end+1;
}
int count = vec.size();
for(int i = 0;i < count;++i){
string word = vec[i];
int len = word.size();
if(len < 1 || len > 3){
continue;
}
if(word[0] == 'A' || word[0] == 'D' || word[0] == 'W' || word[0] == 'S'){
int num = 0;
bool flag = true;
for(int j = 1;j < len;++j){
if(word[j] < '0' || word[j] > '9'){
flag = false;
break;
}
num = num * 10 + word[j] - '0';
}
if(!flag){
continue;
}
if(word[0] == 'A'){
x -= num;
}
else if(word[0] == 'D'){
x += num;
}
else if(word[0] == 'W'){
y += num;
}
else if(word[0] == 'S'){
y -= num;
}
}
}
}
int main(){
string str;
while(getline(cin,str)){
int x = 0,y = 0;
PointMove(str,x,y);
cout<<x<<","<<y<<endl;
}
return 0;
}
<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>
分享到:
相关推荐
华为机试一霸教你过华为机试演讲稿..pdf
华为OD机试(..75.rar
华为机试真题(非牛客网试练题)OD考试真题,不定期更新,文档含代码解答
华为机试一霸教你过华为机试.doc
华为机试一霸教你过华为机试。大菊厂招聘有三关,心理测试,机试,面试。
。。。
。。。
华为机试算法题总结 经验分享
华为机试oj练习题2014
华为机试一霸教你过华为机试e-18页.pdf
本人在准备2014年华为机试的时候,进行整理的,代码全部运行成功。如有错误,请大家见谅。
华为OD系列--华为OD机试
华为机试题目100题练习题
2014重邮华为机试(2013.9.14和2013.9.15)一共三场的题目
华为机试成功归来,与小伙伴们分享下经验
大师兄教你如何过华为机试
贰壹贰叁零华为OD机试.pptx 华为OD机试.pptx 华为OD机试.pptx
华为机试.md
里面有几十道华为历届考过的机试题,可供大家研究和参考,主要是里面的思想,只要理解透了里面的思想,非常有助于机试。
华为机试.py