• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
wjshan0808

Learn from yesterday, Live for today, For a better tomorrow.
 ————wjshan0808

博客园    首页    新随笔    联系   管理    订阅  订阅

杨辉三角 可以不用二维数组的

#include <iostream>
#include <iomanip>

using namespace std;

int main(int argc, char* args[])
{
    //便于窗体显示
    system("mode con cols=128 lines=32");

    int nLine;
    //cin >> nLine;    
    //test
    nLine = 11;
    int* arrLineRecord = new int[nLine + 1]();

    const int nBaseYangValue = 1;
    //行数
    for (int i = 1; i <= nLine; i++)
    {
        //格式化:)
        cout << right << setw(4 * (nLine - i + 1)) << " ";
        int indexCurrentNewValue = 0;
        //列数
        for (int index = 1; index <= i; index++)
        { 
            //取出上一行的 当前索引 值
            int indexCurrentValue = arrLineRecord[index];
            //取出上一行的 当前前一索引 值
            int indexBeforeValue = arrLineRecord[index - 1];
            //还原上一次 新的当前索引值
            arrLineRecord[index - 1] = indexCurrentNewValue;
            
            //新的当前索引值
            indexCurrentNewValue = indexBeforeValue + indexCurrentValue;
            //杨辉三角基值
            indexCurrentNewValue = indexCurrentNewValue == 0 ? nBaseYangValue : indexCurrentNewValue;
            //输出新的当前索引值
            cout << right << setw(8) << indexCurrentNewValue;
            //结束
            if (index == i)
                arrLineRecord[index] = indexCurrentNewValue;

        }
        //换行
        cout << endl;
    }
    delete[] arrLineRecord;

    system("pause");
    return 0;
}

 


posted @ 2017-03-28 17:19  wjshan0808  阅读(439)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3