Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 3/Solution/zh CN: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Basic Pascal Tutorial/Chapter 3/Solution/zh CN

OldNewLine
1+3Ea - 参考答案 (原作者: Tao Yue, 状态: 未更改)
2+
3+**斐波那契数列参考答案**
4+
5+``` pascal
6+(* Author: Tao Yue
7+ Date: 19 July 1997
8+ Description:
9+ Find the first 10 Fibonacci numbers
10+ Version:
11+ 1.0 - original version
12+*)
13+
14+program Fibonacci;
15+
16+var
17+ Fibonacci1, Fibonacci2 : integer;
18+ temp : integer;
19+ count : integer;
20+
21+begin (* Main *)
22+ writeln ('First ten Fibonacci numbers are:');
23+ count := 0;
24+ Fibonacci1 := 0;
25+ Fibonacci2 := 1;
26+ repeat
27+ write (Fibonacci2:7);
28+ temp := Fibonacci2;
29+ Fibonacci2 := Fibonacci1 + Fibonacci2;
30+ Fibonacci1 := Temp;
31+ count := count + 1
32+ until count = 10;
33+ writeln;
34+
35+ (* Of course, you could use a FOR loop or a WHILE loop
36+ to solve this problem. *)
37+
38+end. (* Main *)
39+```
40+
41+**平方问题参考答案**
42+
43+``` pascal
44+(* Author: Tao Yue
45+ Date: 13 July 2000
46+ Description:
47+ Display all powers of two up to 20000, five per line
48+ Version:
49+ 1.0 - original version
50+*)
51+
52+program PowersofTwo;
53+
54+const
55+ numperline = 5;
56+ maxnum = 20000;
57+ base = 2;
58+
59+var
60+ number : longint;
61+ linecount : integer;
62+
63+begin (* Main *)
64+ writeln ('Powers of ', base, ', 1 <= x <= ', maxnum, ':');
65+ (* Set up for loop *)
66+ number := 1;
67+ linecount := 0;
68+ (* Loop *)
69+ while number <= maxnum do
70+ begin
71+ linecount := linecount + 1;
72+ (* Print a comma and space unless this is the first
73+ number on the line *)
74+ if linecount > 1 then
75+ write (', ');
76+ (* Display the number *)
77+ write (number);
78+ (* Print a comma and go to the next line if this is
79+ the last number on the line UNLESS it is the
80+ last number of the series *)
81+ if (linecount = numperline) and not (number * 2 > maxnum) then
82+ begin
83+ writeln (',');
84+ linecount := 0
85+ end;
86+ (* Increment number *)
87+ number := number * base;
88+ end; (* while *)
89+ writeln;
90+
91+ (* This program can also be written using a
92+ REPEAT..UNTIL loop. *)
93+
94+end. (* Main *)
95+```
96+
97+注意,我使用三个常量:numperline为控制显示数目,maxnum为最大值,base为指数;以确保程序可以适应以后。
98+
99+使用常量,而不是文本是一个良好的编程习惯。当程序很长,上千行代码,找起来会很麻烦,你不能简单使用全文搜索和替换。使用常量使得它更简单也易于扩展。
100+
101+另外注意,number变量为`长整型`而不是`整型`,这是因为测试时 `number <= 20000` 时失败了。整型的范围在 `-32768..32767`(尝试使用`整型`,看看会发生什么)。
102+
103+| | | |
104+|----|----|----|
105+| [[Basic Pascal Tutorial/Chapter 3/Programming Assignment/zh CN|上一页]] | [[Basic Pascal Tutorial/Contents/zh CN|目录]] | [[Basic Pascal Tutorial/Chapter 4/Procedures/zh CN|下一页]] |
106+
107+---
108+*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](/wiki-contact).*

Back to history