请帮忙编一个函数(200分)

V

Vinson

Unregistered / Unconfirmed
GUEST, unregistred user!
编写一个函数,用数D与另外三个数A、B、C比较,根据下列条件返回参数:
当A>B>C时,
   D>A   返回 1
   A>D>B  返回 2
   B>D>C  返回 3
   C>D   返回 0
当A>B=C时,
   D>A  返回 1
   A>D>B=C 返回 2
   B=C>D  返回 0
当A=B>C时,
   D>A=B 返回 1
   A=B>D>C 返回 2
   C>D 返回 0
当A=B=C时,
   D>A=B=C 返回 1
A=B=C<D 返回 0

请问应如何编写? 

 
if语句多次比较就可以了
 
function Vinson(D,A,B,C:Integer):Integer;
begin
if ((A>B) and (B>C) and (D>A)) or
((A>B) and (B=C) and (D>A)) or
((A=B) and (B>C) and (D>A) and (A=B)) or
((A=B) and (B=C) and (D>A) and (A=B) and (B=C)) then
Result:=1;
//下略
if ...... then Result:=2;
if ...... then Restlt:=3;
if ...... then Restlt:=0;

end;
 
A,B,C是不是一定满足: A>=B>=C
否则不完备吧。
 
天下会有你这种懒人? 这种问题都要问!
 
if (A>B)and(B>C) then
begin
  if D>A then result:=1;
  if (A>D)and(D>B) then result:=2;
  if (B>D)and(D>C) then result:=3;
  if C>D then result:=0;
end;
if (A>B)and(A=C) then
begin
  if D>A then result:=1;
  if (A>D)and(D>B)and(B=C) result:=2;
  if (B=C)and(C>D) result:=0;
end;
if (A=B)and(B>C) then
begin
  if (D>A)and(A=B) then result:=1;
  if (A=B)and(B>D)and(D>C) then result:=2;
  if C>D then result:=0;
end;
if (A=B)and(A=C) then
begin
  if (D>A)and(A=B)and(B=C) then result:=1;
if (A=B)and(B=C)and(C<D) then result:=0;
end;

唉,好长的语句啊,打的手都疼了,给点辛苦分吧,*^_^*
 
个人认为高人还没出现[?]
 
不就是要找D在4个数中的次序, 然后变换一下么.
Result := 1;
if D < A then Inc(Result);
if D < B then Inc(Result)
if A=B then Dec(Result);
if D < C then Inc(Result)
if B=C then Dec(Result);
Result := Result mod 4;
 
Result := 1;
if D < A then
begin
inc(result);
if D < B then
begin
if A <> B then inc(result);
if D < C then result := 0;
end;
end;
 
提供一个另类方法:

先将四个数用 IntToHex 转换为等长度的字符串
然后将四个字符串都加入一个 TStringList
然后将该 TStringList 排序
最后用 IndexOf 查找 D 对应的字符串在其中的位置

有意思吧:)
 
to beta"
我的A、B、C、D是数值,不是字符噢!
 
我不是说了吗,用 IntToHex 可以将数值转换为字符串啊:)
 
beta的方法确实挺有意思。有创意!
 
如果用if就太多了,如果是N个数相比呢?我写一个简单的算法。
Var I0,I:integer;myNum:array[0..2] of Integer;//2就是要比较的个数-1,如与n个比较,就是n-1
i:=3;//(就是要比较的个数,如与n个比较,就是n);
Mynum[0]:=A;
Mynum[1]:=B;
mynum[2]:=C;
For i0:=0 to 2 do
if A<mynum[i0] then
i:=i-1;
Result:=i;
 
多人接受答案了。
 
顶部