本文共 957 字,大约阅读时间需要 3 分钟。
这道题要求模拟z字形,主要是模拟在什么情况下往哪里走。首先把各个方向的走法用go(direction)函数封装。走的方向是由前一步的前进方向和当前位置决定的。
#includeusing namespace std;#define RIGHT 1#define DOWN 2#define LEFT_DOWN 3#define RIGHT_UP 4int x = 1, y = 1, direction = RIGHT;void go(int direction){ if(direction == RIGHT) y ++; else if(direction == DOWN) x ++; else if(direction == LEFT_DOWN) {x ++; y --;} else {x --; y ++;}}int main(){ int n, times = 1; //第一个点1/1默认算进去了 cin >> n; while(times < n) { go(direction); if(x == 1 && direction == RIGHT_UP) //确定走的方向 { direction = RIGHT; } else if(x == 1 && direction == RIGHT) { direction = LEFT_DOWN; } else if(y == 1 && direction == LEFT_DOWN) { direction = DOWN; } else if(y == 1 && direction == DOWN) { direction = RIGHT_UP; } times ++; } cout << x << "/" << y << endl; return 0;}
转载地址:http://qkuin.baihongyu.com/