用ruby打印杨辉三角

来源:互联网 发布:成都java培训班 编辑:程序博客网 时间:2024/06/05 01:56
def p_tri(n)  count =1  arr = [0,1]  begin     pope = arr.shift    tope = arr[0]    if (pope != 0)      print pope.to_s+' '    else      print "\n"      arr << 0      count += 1    end    sum = tope + pope    arr << sum  end while count < nendp_tri(10)

本来是应该用c语言做的,要求时间和空间复杂度尽量小,但是ruby可以用数组模拟队列的操作,(c/c++里边还需要用数组实现循环队列的顺序存储以保证空间复杂度),总之先(偷个懒)写出来整体逻辑=。=

0 0
原创粉丝点击