[Huawei on-machine exercises] 9. Coordinate movement

Source: Internet
Author: User

Topic

Develop a coordinate calculation tool, a for moving to the left, D for moving to the right, W for moving up, and s for moving down. Moves from the (0,0) point, reads some coordinates from the input string, and outputs the final input to the output file.

Input:

Valid coordinates are a (or D or W or s) + number (within two digits)

to divide between coordinates;

Illegal coordinate points need to be discarded. such as AA10; A1A; ; YAD; such as

The following is a simple example:

A10; S20; W10;d30; X A1A; B10A11;; A10;

Processing process:

Starting point (0,0)

    • A10 = ( -10,0)

    • S20 = ( -10,-20)

    • W10 = ( -10,-10)

    • D30 = (20,-10)

    • x = Invalid

    • A1A = Invalid

    • B10A11 = Invalid

    • An empty does not affect

    • A10 = (10,-10)

Results (10,-10)

Title Category: String
Difficulty: Intermediate
Operating time limit: 10Sec
Memory Limit: 128MByte
Stage: Pre-employment practice
Input:
Line string

Output:
Final coordinates, to, separating

Sample input:
A10; S20; W10;d30; X A1A; B10A11;; A10;

Sample output:
10,-10

Code

/* ---------------------------------------* Date: 2015-06-29* sjf0115* title: Coordinate movement * Source: Huawei on-Machine--------------------------- --------------*/#include <iostream>#include <vector>#include <string>using namespace STD;voidPointmove (stringStrint&x,int&y) {intSize = Str.size ();if(Size = =0){return; }//if     vector<string>VecintStart =0, end =0;//To extract the contents of two semicolons     while(End! =-1) {end = Str.find (";", start);        Vec.push_back (Str.substr (Start,end-start)); Start = end+1; }//while    //Coordinate movement    intCount = Vec.size (); for(inti =0; i < count;++i) {stringWord = vec[i];intLen = Word.size ();if(Len <1|| Len >3){Continue; }//if        if(word[0] ==' A '|| word[0] ==' D '|| word[0] ==' W '|| word[0] ==' S '){intnum =0;BOOLFlag =true;//Calculate the distance traveled             for(intj =1; J < Len;++j) {if(Word[j] <' 0 '|| WORD[J] >' 9 ') {flag =false; Break; }//ifnum = num *Ten+ Word[j]-' 0 '; }//for            //Mobile distance illegal            if(!flag) {Continue; }//if            if(word[0] ==' A ') {x-= num; }//if            Else if(word[0] ==' D ') {x + = num; }//else            Else if(word[0] ==' W ') {y + = num; }//else            Else if(word[0] ==' S ') {y-= num; }//else}//if}//for}intMain () {stringStr//freopen ("C:\\users\\administrator\\desktop\\c++.txt", "R", stdin);     while(Getline (Cin, str)) {intx =0, y =0; Pointmove (Str,x,y);cout<<x<<","<<y<<endl; }//while    return 0;}

[Huawei on-machine exercises] 9. Coordinate movement

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.