要求输入1~26之间的数字 ,输出以下结果。(当输入数字为1~5时,边缘为E,大于5时,中心为A)
实现代码如下:
//、voper,2018-10-24 #include<iostream> using namespace std; class CPRINT { private: int num; public: CPRINT(int n) :num(n){} void print(); }; void CPRINT::print() { int n = num; char k; if (n > 5) { k = 97 + n - 1; } else { k = 'E'; } for (int i = 1; i <= n; i++){ for (int j = 1; j <= 4 * n - 3; j++){ if (j >= 2 * (n - i) + 1 && j <= 2 * (n + i) - 3){ if (j % 2){ if (j < (4 * n - 3) / 2 + 1) cout<< k--; else if (j == (4 * n - 3) / 2 + 1) cout << k; else cout << ++k; } else cout << ""; } else{ if (!(j % 2)) cout << " "; cout << ""; } } cout << endl; } for (int i = n + 1; i <= 2 * n - 1; i++){ for (int j = 1; j <= 4 * n - 3; j++){ if (j >= 2 * (i - n) + 1 && j <= 2 * (3 * n - i) - 3){ if (j % 2){ if (j < (4 * n - 3) / 2 + 1) cout << k--; else if (j == (4 * n - 3) / 2 + 1) cout << k; else cout << ++k; } else cout << ""; } else{ if (!(j % 2)) cout << " "; cout << ""; } } cout << endl; } return; }总结:这类问题主要考察思维能力,即用for和if实现对代码输出随心所欲的掌控能力。
注:本文主要参考doubleguy的一篇文,是在其帮助下实现的。