Turbo

系统教程 行业动态 更新时间:2024-06-14 16:57:40
Turbo-pascal家庭作业(Turbo-pascal home work)

我需要一些Turbo Pascal问题的帮助:

如果N1的每个数字在N2中出现至少一次,反之亦然,则认为两个整数是兄弟。 示例:如果N1 = 1164且N2 = 614,程序将显示N1和N2为兄弟,如果N1 = 504且N2 = 455,程序将显示N1,N2不是兄弟

我的问题是:如何检查2个整数是不是兄弟? 这是我的工作:

function brother(n1, n2: integer): boolean; var test: boolean; ch1, ch2: string; begin chr(n1, ch1); chr(n2, ch2); i := 0; repeat j := 0; i := i + 1; test := false; repeat j := j + 1; if ch1[i] = ch2[j] then test := true; until (test = true) or (j = length(ch2)); until (test = false) or (i = length(ch1)); brother := test; end;

当我运行它时,它总是打印(“整数是兄弟”),即使我把504和455,我想知道错误在哪里。

I need some help in this Turbo Pascal problem:

Two integers are said to be brothers if each digit of N1 appears at least once in N2 and vice versa. Examples: if N1 = 1164 and N2 = 614 the program will display N1 and N2 are brothers, if N1 = 504 and N2 = 455 the program will display N1 and N2 are not brothers

My question is: How to check whether the 2 integers are brothers or not? This is my work:

function brother(n1, n2: integer): boolean; var test: boolean; ch1, ch2: string; begin chr(n1, ch1); chr(n2, ch2); i := 0; repeat j := 0; i := i + 1; test := false; repeat j := j + 1; if ch1[i] = ch2[j] then test := true; until (test = true) or (j = length(ch2)); until (test = false) or (i = length(ch1)); brother := test; end;

when I run this, it always print ("integers are brothers") even when I put 504 and 455, I want to know where the mistake is.

最满意答案

尝试这样的事情:

function contains(s: string; ch: char): boolean; var i: integer; begin contains := false; for i := 1 to length(s) do if s[i] = ch then contains := true; end; function brother(n1, n2: integer): boolean; var test: boolean; ch1, ch2: string; i: integer; begin str(n1, ch1); str(n2, ch2); test := true; { assume "brotherhood" } for i := 1 to length(ch1) do if not contains(ch2, ch1[i]) then test := false; { obviously no brothers after all } { must test both ways, so (n1=455, n2=504) fails too } for i := 1 to length(ch2) do if not contains(ch1, ch2[i]) then test := false; brother := test; end;

而不是,你也可以使用repeat until 。

Try something like this instead:

function contains(s: string; ch: char): boolean; var i: integer; begin contains := false; for i := 1 to length(s) do if s[i] = ch then contains := true; end; function brother(n1, n2: integer): boolean; var test: boolean; ch1, ch2: string; i: integer; begin str(n1, ch1); str(n2, ch2); test := true; { assume "brotherhood" } for i := 1 to length(ch1) do if not contains(ch2, ch1[i]) then test := false; { obviously no brothers after all } { must test both ways, so (n1=455, n2=504) fails too } for i := 1 to length(ch2) do if not contains(ch1, ch2[i]) then test := false; brother := test; end;

Instead of for, you can use repeat until too.

更多推荐

本文发布于:2023-04-13 12:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/4303d115e9f7c58c6d4906f643af0889.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Turbo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!