杨辉三角 By 蜜獾哥.ahk

YanHuiSanJiao:=PrintYanHuiSanJiao(15,Array)
Clipboard:=YanHuiSanJiao
MsgBox % YanHuiSanJiao
PrintYanHuiSanJiao(Count,ByRef Array){
  String:="",Array:=[]
  if !Count
    Return ""
  Loop,% Count {
    Index:=A_Index,Array[Index]:=IsObject(Array[Index])?Array[Index]:[]
    if objLength(Array[Index-1]){
      loopstring:=""
      Loop,% objLength(Array[Index-1])+1
      {
        TEMPVar:=(Array[Index-1,A_Index-1]?Array[Index-1,A_Index-1]:0)+(Array[Index-1,A_Index]?Array[Index-1,A_Index]:0)
        Array[Index].Push(TEMPVar)
        loopstring.=TEMPVar . "  "
      }
      String.=SubStr(Format("{: " (Count-Index)+1 "}",TEMPVar),1,-1) Trim(loopstring," ") . "`n",TEMPVar:=""
    }Else{
      Array[Index].Push(Index)
      String.=Format("{: " (Count-Index)+1 "}",Index) . "`n"
    }
  }

  Return StrReplace(String,A_Space,A_Tab)
}

 

杨辉三角式实现 By 僵尸.ahk

; 没有打印输出方式查看

aaa := sj(10)

sj(Num){
  a := [], b := [], c := []
  loop % Num
  {
    If (A_Index = 1){
      c.Push(1)
      a.Push(c)
      c := []
    } Else If (A_Index = 2){
      c.Push(1)
      c.Push(1)
      a.Push(c)
      b := c
      c := []
    } Else {
      c.Push(1)
      For k,v in b {
        If (k > 1) {
          d := v + b[k-1]
          c.Push(d)
        }
      }
      c.Push(1)
      a.Push(c)
      b := c
      c := []
    }
  }
  For i,n in a
  {
    For ii,nn in n
    {
      If (ii = 1) {
        nn := Format("{:" (Num-i+1) "}", nn)
        nn := RegExReplace(nn, " ", "`t")
        s .= nn
      }
      Else
        s .= "`t`t" nn
    }
    s .= "`n"
  }
  Return s
}

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。