在 oracle pl/sql 中打印帕斯卡三角形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25929360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:31:32  来源:igfitidea点击:

Print pascal triangle in oracle pl/sql

oraclestored-proceduresplsqloracle11goracle10g

提问by S.Roshanth

How to print a pascal triangle with '*' in Oracle using Pl/SQL functions, I am expecting to write in minimal lines of code. Can anyone help me ? This is what I tried,

如何使用 Pl/SQL 函数在 Oracle 中打印带有“*”的帕斯卡三角形,我希望用最少的代码行编写。谁能帮我 ?这是我试过的

begin
for i in 1..5 loop
dbms_output.put_line('');
for j in 1..i loop
dbms_output.put('*');
end loop;
end loop;
end;

I have an answer with two for loops to print a triangle, but I am trying to finish with one for loop using Lpad() and Rpad() functions.

我有两个 for 循环的答案来打印一个三角形,但我正在尝试使用 Lpad() 和 Rpad() 函数完成一个 for 循环。

回答by Gary_W

This is a great opportunity to show how one can approach a problem logically when one does not have any idea how to proceed. No one is here to do homework for you, but we can give some pointers on approaching a problem, then help guide when we see what you come up with.

这是一个很好的机会,可以展示当一个人不知道如何进行时,如何从逻辑上解决问题。没有人在这里为您做作业,但我们可以就解决问题提供一些指导,然后在我们看到您提出的问题时帮助指导。

Start with the end result desired and work backward to figure out what to do to get the desired result. What do you expect the output to be? Something like this (for 4 rows)?:

从所需的最终结果开始,然后向后工作以找出获得所需结果的方法。你期望输出是什么?像这样(4行)?:

   *
  * *
 * * *
* * * *

Do you notice a recurring pattern? "recurring pattern" indicates a loop of some sort will be required. Perhaps if you replace the leading spaces with another character, another recurring pattern will become obvious:

你注意到一个反复出现的模式吗?“重复模式”表示需要某种循环。也许如果你用另一个字符替换前导空格,另一个重复出现的模式将变得明显:

XXX*
XX* *
X* * *
* * * *

So, a recurring pattern within another recurring pattern. What is the relationship between the number of leading spaces to the number of '*'s in the row? The need for a calculation or two may become apparent, depending on the row. That is about all I want to give away, as the point of the assignment is for you to figure it out.

因此,另一个重复模式中的重复模式。行中前导空格的数量与“*”的数量之间有什么关系?根据行的不同,可能需要进行一两次计算。这就是我想给出的全部内容,因为作业的重点是让你弄清楚。

Hopefully this provided a nudge on how to approach solving a problem without giving too much away.

希望这提供了一个关于如何在不放弃太多的情况下解决问题的方法。

回答by S.Roshanth

First I try to draw the pascal triangle with three four loops, it works correctly. Here is the answer..

首先我尝试用三个四个循环绘制帕斯卡三角形,它工作正常。这是答案..

Declare
inc number := 4;
BEGIN

FOR Outter IN 1 .. inc LOOP

          FOR Inner IN 1 .. inc - Outter   LOOP
            dbms_output.put(' ');
          END LOOP;

          FOR Inner IN 1 .. Outter   LOOP
            dbms_output.put('*');
          END LOOP;          

    dbms_output.put_line(' ');

END LOOP;

END;

This is an anonymous block in PL/SQL, and I have an answer with one four loop as well, which correctly print pascal triangle as I expected.

这是 PL/SQL 中的一个匿名块,我也有一个四循环的答案,它按我的预期正确打印了 pascal 三角形。

DECLARE 
  row_num_ NUMBER := 5; 
BEGIN 
    FOR i IN 0..row_num_ LOOP 
      DBMS_OUTPUT.PUT_LINE(' ' || LPAD(' ', row_num_ - i, ' ') || RPAD('*', 2 * i - 1, '*')); 
    END LOOP;  
END;

Thank you friends for your great support..

感谢朋友们的大力支持。。

回答by YD PC Solutions

Declare
n number;
BEGIN
n:=&n;
FOR i IN 1 .. n LOOP


      FOR j IN 1 .. i   LOOP
        dbms_output.put('* ');
      END LOOP;          

    dbms_output.put_line(' ');

END LOOP;

END;

回答by shivannarayana reddy pagadala

How to get pyramid shape by using procedure? The following code can achieve that:

如何使用程序获得金字塔形状?以下代码可以实现:

declare
    s2 varchar2(38);
    s3 varchar2(38);
    m number(10):=6;
    l number(10);
    s1 varchar2(38);
    a varchar2(38);
    b varchar2(38);
begin
    l:=((m*2)/2)+1;
    for r in 0..m
    loop
        s2:=trim(both ' ' from s3)||' '||r;
        a:=lpad(s2,l+r+1,'*')||' '||'';
        b:=rpad(a,(m*2)+r+1,'*');
        s1:=b;
        dbms_output.put_line(s1);
        s3:=trim(both '*' from s1);
    end loop;
end;
/

We will then get this result:

然后我们会得到这样的结果:

****** 0 ****
******0 1 ****
*****0 1 2 ****
****0 1 2 3 ****
***0 1 2 3 4 **** 
**0 1 2 3 4 5 ****
*0 1 2 3 4 5 6 ****

回答by vinay

declare v_str varchar2(10) := '*'

begin
   for i in 1..5 loop
      v_str:= rpad(v_str, i, '*');
      dbms_output.put_line(v_str);
   end loop;
end;
*
**
***
****
*****