HJ17 坐标移动

摘要
Title: HJ17 坐标移动
Tag: 字符串输入、atoi、getline
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

HJ17 坐标移动

  • 题意

    开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。

  • 思路

    熟练运用getline分隔字符串

    在C中,<< 运算符用于输出数据到流中,包括标准输出流(cout)和其他类型的流,例如文件流或字符串流。这种运算符的设计是为了与 >> 运算符(用于从流中读取数据)形成对称性。
    这个设计的目的是提供一种直观的方式来操作流,使代码易于理解和编写。当你使用 << 运算符时,你可以将数据从程序输出到流中,就像你在控制台或文件中看到的输出一样。这样的操作符重载让C
    的输入输出操作看起来更像自然语言,使代码更易于阅读和维护。
    总之,<< 运算符表示输出(写入)数据到流中,而 >> 运算符表示从流中读取数据,这种设计是为了让C++代码更具可读性和易用性。

  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    #include <bits/stdc++.h>
    #include <cctype>
    #include <sstream>
    using namespace std;
    #define int long long
    #undef int

    #define SZ(X) ((int)(X).size())
    #define ALL(X) (X).begin(), (X).end()
    #define IOS \
    ios::sync_with_stdio(false); \
    cin.tie(nullptr); \
    cout.tie(nullptr)
    #define DEBUG(X) cout << #X << ": " << X << '\n'

    const int M = 70, N = 4e4 + 10, INF = 0x3f3f3f3f;


    signed main() {
    IOS;
    int x = 0, y = 0;
    string s;
    getline(cin, s);

    stringstream ss;
    ss << s;

    while (getline(ss, s, ';')) {
    if (SZ(s) == 2 && isdigit(s[1]) || SZ(s) == 3 && isdigit(s[1]) &&
    isdigit(s[2])) {
    int num = atoi(s.substr(1).c_str());

    switch (s[0]) {
    case 'A':
    x -= num;
    break;
    case 'S':
    y -= num;
    break;
    case 'W':
    y += num;
    break;
    case 'D':
    x += num;
    break;
    }
    }


    }

    cout << x << "," << y;


    return 0;
    }
使用搜索:谷歌必应百度