Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 3/Solution/zh CN

Sent here from Solution 3/zh CN.

English · Français · Български · 中文(中国) · 日本語

3Ea - 参考答案 (原作者: Tao Yue, 状态: 未更改)

斐波那契数列参考答案

pascal
(* Author:    Tao Yue
   Date:      19 July 1997
   Description:
      Find the first 10 Fibonacci numbers
   Version:
      1.0 - original version
*)

program Fibonacci;

var
   Fibonacci1, Fibonacci2 : integer;
   temp : integer;
   count : integer;

begin    (* Main *)
   writeln ('First ten Fibonacci numbers are:');
   count := 0;
   Fibonacci1 := 0;
   Fibonacci2 := 1;
   repeat
      write (Fibonacci2:7);
      temp := Fibonacci2;
      Fibonacci2 := Fibonacci1 + Fibonacci2;
      Fibonacci1 := Temp;
      count := count + 1
   until count = 10;
   writeln;

   (* Of course, you could use a FOR loop or a WHILE loop
      to solve this problem. *)

end.     (* Main *)

平方问题参考答案

pascal
(* Author:    Tao Yue
   Date:      13 July 2000
   Description:
      Display all powers of two up to 20000, five per line
   Version:
      1.0 - original version
*)

program PowersofTwo;

const
   numperline = 5;
   maxnum = 20000;
   base = 2;

var
   number : longint;
   linecount : integer;

begin    (* Main *)
   writeln ('Powers of ', base, ', 1 <= x <= ', maxnum, ':');
   (* Set up for loop *)
   number := 1;
   linecount := 0;
   (* Loop *)
   while number <= maxnum do
      begin
         linecount := linecount + 1;
         (* Print a comma and space unless this is the first
            number on the line *)
         if linecount > 1 then
            write (', ');
         (* Display the number *)
         write (number);
         (* Print a comma and go to the next line if this is
            the last number on the line UNLESS it is the
            last number of the series *)
         if (linecount = numperline) and not (number * 2 > maxnum) then
            begin
               writeln (',');
               linecount := 0
            end;
         (* Increment number *)
         number := number * base;
      end;  (* while *)
   writeln;

   (* This program can also be written using a
      REPEAT..UNTIL loop. *)

end.     (* Main *)

注意,我使用三个常量:numperline为控制显示数目,maxnum为最大值,base为指数;以确保程序可以适应以后。

使用常量,而不是文本是一个良好的编程习惯。当程序很长,上千行代码,找起来会很麻烦,你不能简单使用全文搜索和替换。使用常量使得它更简单也易于扩展。

另外注意,number变量为长整型而不是整型,这是因为测试时 number <= 20000 时失败了。整型的范围在 -32768..32767(尝试使用整型,看看会发生什么)。

上一页 目录 下一页

From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 3/Solution/zh CN", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Edited on wiki.freepascal.org by 4 people, 2013–2022

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
WangyouworldCreated page
Swen (minor)
Wangyouworld (minor)
TrevFixed syntax highlighting; deleted category included in page template (minor)
Kai BurghardtKai Burghardt moved page Solution 3/zh CN to Basic Pascal Tutorial/Chapter 3/Solution/zh CN: tidy up main name space: create subpage hierarchy for basic Pascal tutorial [cf. [[Special: PermaLink/14... (minor)
Kai Burghardtbypass redirects [cf. discussion]
Kai Burghardtbypass [previously missed] redirect [cf. discussion]
Kai Burghardtbypass language bar/categorization template redirect [cf. discussion] (minor)

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.