帕斯卡三角形一圈 [英] Pascals triangle in one loop

查看:97
本文介绍了帕斯卡三角形一圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用一个循环写出帕斯卡的三角形?我已经使用多个循环编写了它,并且工作正常.

Is it possible to write the pascal's triangle by using one loop ? I have written it by using more than one loop and it is working fine.

推荐答案

哎呀,我将我的评论作为答案:

Heck, I'll make my comment an answer:

作为提示,我将创建一个方法,将要生成的三角形的行数作为输入,然后在该方法的开头计算要转换为的项目总数,然后让您for循环遍历所有项目.在循环内部,您可以轻松计算出您所在的行和所在的列,然后使用此信息来创建项目值.

As a hint, I would create a method that takes as input the number of rows of the triangle you want to produce, then at the beginning of the method calculate total number of items this will translate out to, and then have your for loop loop through all the items. Inside the loop, you can easily calculate which row you're on and which column you're on, and then use this information to create your item value.

这篇关于帕斯卡三角形一圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆