[華為上機練習題]9.座標移動

來源:互聯網
上載者:User

標籤:華為上機練習題

題目

開發一個座標計算工具, A表示向左移動,D表示向右移動,W表示向上移動,S表示向下移動。從(0,0)點開始移動,從輸入字串裡面讀取一些座標,並將最終輸入結果輸出到輸出檔案裡面。

輸入:

合法座標為A(或者D或者W或者S) + 數字(兩位以內)

座標之間以;分隔。

非法座標點需要進行丟棄。如AA10; A1A; ; 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

代碼

/*---------------------------------------*   日期:2015-06-29*   SJF0115*   題目:座標移動*   來源:華為上機-----------------------------------------*/#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;    // 把兩個分號之間的內容提取出來    while(end != -1){        end = str.find(";",start);        vec.push_back(str.substr(start,end-start));        start = end+1;    }//while    // 座標移動    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        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;                }//if                num = num * 10 + word[j] - ‘0‘;            }//for            // 移動距離非法            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}int main(){    string str;    //freopen("C:\\Users\\Administrator\\Desktop\\c++.txt","r",stdin);    while(getline(cin,str)){        int x = 0,y = 0;        PointMove(str,x,y);        cout<<x<<","<<y<<endl;    }//while    return 0;}

[華為上機練習題]9.座標移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.