Powered by:NEFU AB-IN
Link
文章目录
- HJ17 坐标移动
- 题意
- 思路
- 代码
HJ17 坐标移动
-
题意
开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。
-
思路
熟练运用getline分隔字符串
在C++中,<< 运算符用于输出数据到流中,包括标准输出流(cout)和其他类型的流,例如文件流或字符串流。这种运算符的设计是为了与 >> 运算符(用于从流中读取数据)形成对称性。
这个设计的目的是提供一种直观的方式来操作流,使代码易于理解和编写。当你使用 << 运算符时,你可以将数据从程序输出到流中,就像你在控制台或文件中看到的输出一样。这样的操作符重载让C++的输入输出操作看起来更像自然语言,使代码更易于阅读和维护。
总之,<< 运算符表示输出(写入)数据到流中,而 >> 运算符表示从流中读取数据,这种设计是为了让C++代码更具可读性和易用性。 -
代码
#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; }