算法题,打印沙漏
Posted huaye37
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了算法题,打印沙漏相关的知识,希望对你有一定的参考价值。
#include <iostream> using namespace std; int sak(int z) { return 1 + 4 * z + 2 * z * z; } int main() { int x; char a; cin >> x >> a; //cout << x << a << endl; int n = 0; //cout << sak(n + 1) << endl; while(!((x >= sak(n)) && (x < sak(n + 1)))) { n++; //cout << n << endl; } //cout << n; int k = n; int r = 0; for (; k != 0; k--,r++) { int s = 2 * k + 1; for (int i = 0; i < r; i++) { cout << " "; } for (int j = 0; j < s; j++) { cout << a; } cout << endl; } for (int i = 0; i < r; i++) { cout << " "; } cout << a << endl; k++; r--; for (; k != n + 1; r--, k++) { int s = 2 * k + 1; for (int i = 0; i < r; i++) { cout << " "; } for (int j = 0; j < s; j++) { cout << a; } cout << endl; } cout << x - sak(n); system("pause"); return 0; }
以上是关于算法题,打印沙漏的主要内容,如果未能解决你的问题,请参考以下文章