首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Turbo-pascal家庭工作

Turbo-pascal家庭工作
EN

Stack Overflow用户
提问于 2017-07-19 03:52:06
回答 2查看 135关注 0票数 2

在这个Turbo问题上我需要一些帮助:

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

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

代码语言:javascript
复制
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时,我也想知道错误在哪里。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-22 21:12:57

试着做这样的事情:

代码语言:javascript
复制
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;

而不是for,您也可以使用repeat until

票数 1
EN

Stack Overflow用户

发布于 2017-07-19 03:54:28

您需要pascal的else/if变体,在那里您必须检查它是否相等。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45180669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档