Sicily 1797. Do solve it directly
水题,看明白题目就可以了。
题意是,控制一个机器人行动,s,r,l为控制机器人操作
给一个函数,先按它给的定义计算出该函数,然后解析该字符串
#include <iostream> #include <string> using namespace std; string z(int , int); struct point { int x; int y; int now_direction; }; point analysis(string); int main() { int n; int a,b; point p; //freopen("C:\\Users\\Haojian\\Desktop\\test.txt", "r", stdin); cin >> n; for (int i = 0; i < n; i++) { cin >> a >> b; p = analysis(z(a,b)); cout << p.x << " " << p.y << endl; } return 0; return 0; } point analysis(string s) { point p= {0,0,0}; for (int i = 0; i < s.size(); i++) { switch (s[i]) { case 'r': p.now_direction = (p.now_direction+1)%4; break; case 'l': p.now_direction = (p.now_direction-1)%4; break; case 's': switch (p.now_direction) { case 0: p.y++; break; case 1: p.x++; break; case 2: p.y--; break; case 3: p.x--; break; } break; } } return p; } string z(int a, int b) { string s; if (a <= 0 || b <= 0 ) { s = ""; return (s); } else { s = "s" + z(a-b,b) + "r" + z(b-a,a); return s; } }