解题思路:递归, 4以下写的很苦, 不优化了
注意事项:格式
参考代码:
#include<iostream> #include<limits> #include<cstring> #include<cmath> using namespace std; const int N = 1e5 + 10; char a[N]; int m; void print(int i){ printf("step%2d:",i); for(int i = 1; i <= 2 * m + 2; i ++){ cout << a[i]; } cout << endl; } void dfs(int n,int l){ if(n > 4){ a[2*n + 1] = a[n]; a[2*n + 2] = a[n + 1]; a[n] = a[n + 1] = '-'; print(l++); a[n] = a[2 * n - 1]; a[n + 1] = a[2 * n - 2]; a[2 * n - 1] = a[2* n ] = '-'; print(l++); dfs(n - 1, l); } if(n == 4){ a[2*n + 1] = a[n]; a[2*n + 2] = a[n + 1]; a[n] = a[n + 1] = '-'; print(l++); a[n] = a[2 * n ]; a[n + 1] = a[2 * n + 1]; a[2 * n] = a[2* n + 1] = '-'; print(l++); a[2*n] = a[n- 2]; a[2*n + 1] = a[n- 1]; a[n-2] = a[n - 1] = '-'; print(l++); n--; a[n-1] = a[2*n + 1]; a[n] = a[2*n + 2]; a[2*n + 1] = a[2*n + 2] = '-'; print(l ++); a[2*n + 1] = a[n - 2] ; a[2*n + 2] = a[n - 1] ; a[n - 2] = a[n - 1] = '-'; print(l ++); return; } } int main() { cin >> m; for(int i = 1; i <= m; i ++){ a[i] = 'o'; a[i + m] = '*'; } a[2*m + 1] = a[2*m + 2] = '-'; print(0); dfs(m,1); return 0; }
0.0分
1 人评分
C语言训练-大、小写问题 (C语言代码)浏览:649 |
WU-输入输出格式练习 (C++代码)浏览:1133 |
校门外的树 (C语言代码)浏览:733 |
简单的a+b (C语言代码)浏览:626 |
完数 (C语言代码)浏览:757 |
sizeof的大作用 (C语言代码)浏览:1592 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:627 |
C语言程序设计教程(第三版)课后习题12.3 (C语言代码)浏览:587 |
敲七 (C++代码)浏览:1119 |
1231题解(注意理解“输入多个测试实例”)浏览:830 |