[Huawei machine trial exercise questions] 9. coordinate movement, Huawei exercise questions

Source: Internet
Author: User

[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)

  • A10 = (-10, 0)

  • S20 = (-10,-20)

  • W10 = (-10,-10)

  • D30 = (20,-10)

  • X = invalid

  • A1A = invalid

  • B10A11 = invalid

  • An empty value does not affect

  • A10 = (10,-10)

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.