[Huawei machine trial exercise questions] 9. coordinate movement, Huawei exercise questions
Question
Develop A coordinate computing tool. A indicates moving to the left, D indicates moving to the right, W indicates moving up, and S indicates moving down. Start from (0, 0), read some coordinates from the input string, and output the final input result to the output file.
Input:
Valid coordinate is A (or D, W, or S) + number (within two digits)
The coordinates are separated.
Invalid coordinate points must be discarded. Such as AA10; A1A; YAD.
The following is a simple example:
A10; S20; W10; D30; X; A1A; B10A11; A10;
Processing Process:
Start Point (0, 0)
Result (10,-10)
Question type: String
Difficulty: Intermediate
Run Time Limit: 10Sec
Memory limit: 128 MByte
Stage: Pre-Employment exercises
Input:
One line of string
Output:
Final coordinates separated by commas (,)
Sample input:
A10; S20; W10; D30; X; A1A; B10A11; A10;
Sample output:
10,-10
Code
/* ------------------------------------- * Date: 2015-06-29 * Author: SJF0115 * Subject: coordinate movement * Source: huawei machine --------------------------------------- */# 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;} // if vector <string> vec; int start = 0, end = 0; // extract the content between two semicolons while (end! =-1) {end = str. find (";", start); vec. push_back (str. substr (start, end-start); start = end + 1;} // while // move the 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; // calculate the moving distance for (int j = 1; j <len; ++ j) {if (word [J] <'0' | word [j]> '9') {flag = false; break ;} // if num = num * 10 + word [j]-'0';} // for // if (! Flag) {continue;} // if (word [0] = 'A') {x-= num ;} // if else if (word [0] = 'D') {x + = num;} // else if (word [0] = 'W ') {y + = num;} // else if (word [0] = 's') {y-= num ;} // else} // if} // for} int main () {string str; // freopen ("C: \ Users \ Administrator \ Desktop \ c00000000.txt "," r ", stdin); while (getline (cin, str) {int x = 0, y = 0; pointMove (str, x, y); cout <x <"," <y <endl;} // while return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.