c++金字塔程序
#include <cstdlib> #include <iostream> using namespace std; #define MAXLEN 40 void printline(int i) {//i用来指示当前行剩余钻石的个数 char cstr[MAXLEN] = {0}; memset(cstr, '\0', MAXLEN); int mid = MAXLEN / 2; int j, k; //初始化,根据奇偶决定是否打印第一行 if (i % 2 != 0) { cstr[mid] = '*'; j = mid - 2; k = mid + 2; i --; } else { j = mid - 1; k = mid + 1; } //从奇数的第二行和偶数的第一行开始,打印算法相同 while (i > 0) { cstr[j] = cstr[k] = '*'; j -= 2; k += 2; i -= 2; } for (int x = 0; x < MAXLEN; x++) { cout<<cstr[x]; } cout<<endl; } int main(int argc, char *argv[]) { for (int i = 1; i < 20; i++) { printline(i); } system("PAUSE"); return EXIT_SUCCESS; }