| 发表于:2007-03-17 18:56:292楼 得分:20 |
using system; using system.collections.generic; using system.text; namespace consoleapplication5 { class program { static void main(string[] args) { while (true) { string numstr = console.readline(); try { int num = int32.parse(numstr); console.write(getprintstring(num)); } catch { break; } } } static string getprintstring(int a) { if (a <= 0) { return " "; } if (a == 1) { return "* "; } stringbuilder sb = new stringbuilder(); for (int i = 0; i < a; i++) { string left = " ";//左边的空格 for (int j = i; j < a - 1; j++) { left += " "; } if (i == 0) { sb.appendformat( "{0}*\r\n ", left); continue; } string middle = " "; middle += i == a - 1 ? "* " : " "; for (int j = 0; j < i; j++) { if (j > 0) middle += i == a - 1 ? "** " : " "; } sb.appendformat( "{0}*{1}*\r\n ", left, middle); } return sb.tostring(); } } } | | |
|