1

我已经在 PLSQL 中编写了代码。在哪里我需要检查数字的立方和是否=自己编号。

我已经尝试遵守算法,仍然有一些错误。请帮助。我是 PLSQL 的新手。

以下是我的代码:

   set serveroutput on;



Declare 

    I number(4);
    Sum number(4):=0;
    C number(15):=10;   

Begin   
    for I in 1..999
    loop

    --      dbms_output.put_line(I);



        Sum:=power(mod(I,C),3);

        while mod(I,C)
        loop

            Sum:=power(mod(mod(I,C),C),3);


            C:=C*10;

        end loop;       


        if Sum=I then

            dbms_output.put_line(I);        

        end if;

    end loop;

End;

/
4

1 回答 1

3

sum is a key word in plsql, you should not be using that as a variable name.

Here is the solution for your problem:

SET serveroutput ON format wraped;
DECLARE
  i    INTEGER := 153;
  j    INTEGER;
  summ INTEGER := 0;
BEGIN
  j      := i;
  WHILE i > 0
  LOOP
    summ := summ + MOD(i,10) ** 3;
    i    :=  FLOOR (i  / 10 );
  END LOOP;
  IF summ = j THEN
    dbms_output.put_line('Sum of cubes of digits is EQUAL to the number');
  ELSE
    dbms_output.put_line('Sum of cubes of digits is NOT EQUAL to the number');
  END IF;
END;

The solution works for any INTEGER, i, which is NUMBER(38).

于 2010-11-28T15:24:10.910 回答