如何使用PL / SQL打印星形三角形

时间:2017-07-18 09:57:47

标签: plsql oracle11g

实际上是否可以在PL / SQL中创建如下所示的星形三角形。我知道这可以在任何其他编程语言(如C,C ++,Java)中轻松完成,但想知道它是否真的只用SQL或PL / SQL。这是我的作业,我应该使用条件子句(IF THEN ELSE),循环(FOR,WHILE)。

      *
     ***
    *****
   *******
  *********
 ***********
*************

*****
 ***
  *

4 个答案:

答案 0 :(得分:2)

试试这个。 第一个循环将以三角形打印星形,第二个循环将反转它。

PL / SQL中:

BEGIN
   FOR i IN 1 .. :p
   LOOP
      DBMS_OUTPUT.put_line (LPAD (LPAD ('*', i, '*'), :p + 1, ' '));

   END LOOP;
    FOR i IN 1 .. :p
   LOOP
      DBMS_OUTPUT.put_line (LPAD (LPAD ('*', :p-i, '*'), :p + 1, ' '));
   END LOOP;
END;

SQL中:

    SELECT LPAD (LPAD ('*', level, '*'), :p + 1, ' ') a
      FROM DUAL
CONNECT BY LEVEL <= :p;

答案 1 :(得分:2)

这可以完全在sql(在Oracle中)完成,如下所示:

SELECT RPAD(' ', :p_num_triangle_rows - LEVEL) || RPAD('*', LEVEL * 2 -1, '*') || RPAD(' ', :p_num_triangle_rows - LEVEL) triangle
FROM   dual
CONNECT BY LEVEL <= :p_num_triangle_rows
ORDER BY CASE WHEN :p_ascending_or_descending = 'a' THEN LEVEL END ASC,
         CASE WHEN :p_ascending_or_descending = 'd' THEN LEVEL END DESC;

p_num_triangle_rows:= 20,p_ascending_or_desc:=&#39; a&#39;:

TRIANGLE
--------------------------------------------------------------------------------
                   *
                  ***
                 *****
                *******
               *********
              ***********
             *************
            ***************
           *****************
          *******************
         *********************
        ***********************
       *************************
      ***************************
     *****************************
    *******************************
   *********************************
  ***********************************
 *************************************
***************************************

p_num_triangle_rows:= 3,p_ascending_or_desc:=&#39; d&#39;:

TRIANGLE
--------------------------------------------------------------------------------
*****
 ***
  *

ETA:这是一个PL / SQL版本,可以完成你之后的工作:

DECLARE
  PROCEDURE produce_triangle_rows (p_num_triangle_rows IN NUMBER,
                                   p_ascending_or_descending IN VARCHAR2 DEFAULT 'a')
  IS
  BEGIN
    dbms_output.put_line('p_num_triangle_rows = '|| p_num_triangle_rows ||', p_ascending_or_descending = ' || p_ascending_or_descending);
    FOR i IN 1..p_num_triangle_rows
    LOOP
      CASE WHEN p_ascending_or_descending = 'a' THEN
                dbms_output.put_line(RPAD(' ', p_num_triangle_rows - i) || RPAD('*', i * 2 - 1, '*') || RPAD(' ', p_num_triangle_rows - i));
           WHEN p_ascending_or_descending = 'd' THEN
                dbms_output.put_line(RPAD(' ', i - 1) || RPAD('*', 2 * (p_num_triangle_rows - i) + 1, '*') || RPAD(' ', i - 1));
      END CASE;
    END LOOP;
  END produce_triangle_rows;
BEGIN
  produce_triangle_rows(p_num_triangle_rows => 5,
                        p_ascending_or_descending => 'a');

  produce_triangle_rows(p_num_triangle_rows => 3,
                        p_ascending_or_descending => 'd');
END;
/


p_num_triangle_rows = 5, p_ascending_or_descending = a
    *    
   ***   
  *****  
 ******* 
*********

p_num_triangle_rows = 3, p_ascending_or_descending = d
*****
 *** 
  *  

请注意,我已将程序包装在匿名块中,因此我可以使用不同的参数调用它。您只需自己创建produce_triangle_rows过程,然后适当地调用它。

答案 2 :(得分:0)

试试这个

declare @count int,@num int,@num1 int, @space int, @str varchar(50)
set @count = 3 set @num = 1
while(@num<=@count)
begin
  set @num1 = 0 set @space = @count-@num
  while (@num1<@num)
  begin
   if @str is null
    set @str = '* '
   else
    set @str = @str+'* '   set @num1 = @num1+1
  end
print (space(@space)+@str)
set @num = @num+1   set @str = null
end

或者

Declare @x varchar(20)=0,@y varchar(20)=5
while(@y>0)
begin
print space(@y)+replicate('*',@x)+replicate('*',@x+1)
set @y=@y-1
set @x=@x+1
end

答案 3 :(得分:0)

我发现问题很有趣,所以我在PostgreSQL中解决了它。不幸的是重复字符似乎并不是一个标准化的功能。以下是用于最常见的RDBMS的语法:

  • PostgreSQL:repeat('*', n)
  • Oracle:rpad('', n, '*')
  • MS SQL Server:replicate('*', n)
  • MySQL:repeat('*', n)

首先创建一些数字表格,直到你能想象到的最大三角形:

create table n10 (n) as
  select 0 union
  select 1 union
  select 2 union
  select 3 union
  select 4 union
  select 5 union
  select 6 union
  select 7 union
  select 8 union
  select 9;

create table n1000 (n) as
  select 100 * a1.n + 10 * a2.n + a3.n
  from n10 a1 cross join n10 a2 cross join n10 a3;

然后制作一个大小为5的三角形(用你想要的0到1000之间的任意数字替换5):

with s (s) as (select 5)
select
  repeat(' ', s - n - 1) ||
  repeat('*', 2 * n + 1) ||
  repeat(' ', s - n - 1)
from n1000 cross join s
where n < s
order by n;

with s (s) as (select 5)
select
  repeat(' ', s - n - 1) ||
  repeat('*', 2 * n + 1) ||
  repeat(' ', s - n - 1)
from n1000 cross join s
where n < s
order by n desc;