zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    数据类型
    python安装
    计算机基础
    Ajax--1
    ASP.net+MVC--2
    More lumber is required
    History Grading
    strcmp() Anyone?
    How Many Points of Intersection?
    Remember the Word
  • 原文地址:https://www.cnblogs.com/lihaozy/p/2700681.html
Copyright © 2011-2022 走看看