本文主要是介绍洛谷P1498,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目传送门
挺有意思的一道题目(本蒟蒻用的递归,在洛谷题解中看到有daolao用杨氏三角做%%%%%%%)
思路:
(图中,H为三角形的高,W为三角形的宽)
观察可以发现,如果我们确定一个三角形的基准点(row,col),那么大图形的基准点和小图形的基准点是有规律可循的:以((1<<n)-1,0)这个基准点开始向上递归寻找子图形,当n=1时,将图形存入数组,结束该层
代码:
#include<iostream>
#include<string>
using namespace std;
const int MAX = 1025;
char s[MAX][MAX];
void f(int x, int y, int n)
{if (n == 1) {s[x][y] = '/';s[x][y + 1] = '_';s[x][y + 2] = '_';s[x][y + 3] = '\\';s[x - 1][y + 1] = '/';s[x - 1][y + 2] = '\\';return;}f(x, y, n - 1);f(x, y+(1<<n), n-1);f(x - (1 << (n - 1)), y + (1 << (n - 1)), n - 1);
}
int main()
{int n;cin >> n;f((1 << n)-1, 0, n);for (int i = 0; i < (1 << n); i++) {for (int j = 0; j < (1<<(n+1)); j++) {if (!s[i][j]) cout << " ";else cout << s[i][j];}cout << endl;}return 0;
}
这篇关于洛谷P1498的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!