程序问题是这样的:
友好号码:
有一些号码被称为“友好号码”。根据定义,友好数字是两个不同的数字,其真因数之和等于另一个数字。
例如:最小的友好数字对是 220 和 284。220 的真因数是:1,2,4,5,10,11,20,22,24,44,55 和 110。这些除数之和为 284。
另一方面,284 的真因数为:1、2、4、71 和 142。这些因数之和为 220。
因此,220和284被称为“友好号码”。编写一个程序来查找并打印前两打友好数字。
友好号码列表可以找到here 。我正在尝试调用 80620 和 389924。
我的源代码在这里:
program Friendly Numbers;
type List=Array[1..50] of Integer;
var Num,Sum,Sum2,C,C2,C3,C4,CheckNum,LastNum:Integer;
NumUsed:String;
CheckedNum,CheckedSum:List;
begin
Num:=200;
while C3<=23 Do
begin
while C<Num Do
begin
C:=C+1;
if (C<>Num) and (Num mod C=0) then Sum:=Sum+C;
end;
while C2<Sum Do
begin
C2:=C2+1;
if (C2<>Sum) and (Sum mod C2=0) then Sum2:=Sum2+C2;
end;
if Num=Sum2 then
begin
for C4:=1 to 35 do
begin
if CheckedNum[C4]=C2 then NumUsed:='T';
if CheckedSum[C4]=Num then NumUsed:='T';
end;
if (C2<>LastNum) and (NumUsed<>'T') and (Num<>C2) then
begin
C3:=C3+1;
Writeln(C3,')',Num:8,C2:8);
LastNum:=Num;
end;
Checknum:=CheckNum+1;
CheckedNum[CheckNum]:=Num;
CheckedSum[CheckNum]:=C2;
end;
NumUsed:='F';
Num:=Num+1;
Sum:=0;
Sum2:=0;
C:=0;
C2:=0;
end;
Readln;
end.
输出:
1) 220 284 2) 1184 1210 3) 2620 2924 4) 5020 5564 5) 6232 6368 6) 10744 10856 7) 12285 14595 8) 17296 18416 9) 63020 76084 10) 66928 66992 11) 67095 71145 12) 69615 87633 13) 79750 88730 14) 100485 124155 15) 122265 139815 16) 122368 123152 17) 141664 153176 18) 142310 168730 19) 171856 176336 20) 176272 180848 21) 176336 171856
*176336 repeats on the preceding line, when 176336 already appeared on the right column two lines before.
22) 180848 176272 23) 185368 203432 24) 196724 202444
最佳答案
您存储到数组中的内容与您在屏幕上显示的内容之间存在差异。您正在存储每一对,但仅显示(或者至少尝试显示)唯一的对。并且您枚举显示的对与存储的对不同。除此之外,当检查是否已经遇到(并显然显示)某个数字时,您只搜索数组的前 35 个元素。
现在,如果您决定显示程序发现的所有对,即查看数组中存储的内容以及索引下的内容,您可以看到以下内容:
1) 220 284
2) 284 220
3) 496 496
4) 1184 1210
5) 1210 1184
6) 2620 2924
7) 2924 2620
8) 5020 5564
9) 5564 5020
10) 6232 6368
11) 6368 6232
12) 8128 8128
13) 10744 10856
14) 10856 10744
15) 12285 14595
16) 14595 12285
17) 17296 18416
18) 18416 17296
19) 63020 76084
20) 66928 66992
21) 66992 66928
22) 67095 71145
23) 69615 87633
24) 71145 67095
25) 76084 63020
26) 79750 88730
27) 87633 69615
28) 88730 79750
29) 100485 124155
30) 122265 139815
31) 122368 123152
32) 123152 122368
33) 124155 100485
34) 139815 122265
35) 141664 153176
36) 142310 168730
37) 153176 141664
38) 168730 142310
39) 171856 176336 <<<
40) 176272 180848
41) 176336 171856
42) 180848 176272
43) 185368 203432
44) 196724 202444
...
注意第一次遇到 176336 的时间:在#39 对中。现在你明白为什么你的程序在第二次显示数字时似乎忽略它了:仅仅是因为它不费心去搜索第 35 个元素之外的数组。
另一方面,如果您只存储显示的内容,35可能就足够了。
关于windows - 我在一个列中有一个重复的结果,即使我有针对它的规则。如何消除这种重复出现的情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14207526/