哪位老兄帮我将 DELPHI源码转为BCB源码?(100分)

T

tdKno

Unregistered / Unconfirmed
GUEST, unregistred user!
以下一段代码是从这的数据结构中摘取出来,哪位老兄将它改为BCB啊?谢谢
type
IA=array of Integer;
function GenRandArray(MinNum,MaxNum,MinValue,MaxValue:Integer):IA;
var
i,n:Integer;
begin
Randomize;
n:=MinNum+Random(MaxNum-MinNum+1);
SetLength(Result,n);
for i:=0 to n-1do
Result:=MinValue+Random(MaxValue-MinValue+1);
end;
function FindMaxSum(A:IA;Sum:Integer):Integer;
var
MinLeft:Integer;
Used:array of Boolean;
procedure QuickSortIA(L, R: Integer);
//从小到大排序
var
I, J, P, M: Integer;
begin
repeat
I := L;
J := R;
P := (L + R) shr 1;
repeat
while A<A[P]do
Inc(I);
while A[J]>A[P]do
Dec(J);
if I <= J then
begin
M:=A;
A:=A[J];
A[J]:=M;
if P = I then
P := J
else
if P = J then
P := I;
Inc(I);
Dec(J);
end;
until I > J;
if L < J then
QuickSortIA(L, J);
L := I;
until I >= R;
end;
procedure FindLeft(LeftSum,LeftCount:Integer);
var
i,n,s:Integer;
Str:String;
begin
if (A[0]>LeftSum) or Used[0] then
begin
if LeftSum<MinLeft then
begin
MinLeft:=LeftSum;
Str:='';
s:=0;
n:=0;
for i:=0 to High(A)do
if Used then
begin
Str:=Str+IntToStr(A)+' ';
Inc(s,A);
Inc(n);
end;
Str:=Str+#13#10+IntToStr(s);
Form1.Memo1.Lines.Add(Str);
Application.ProcessMessages;
//如果已经达到理想和值,或者已经耗尽了所有元素——该结束了
if (LeftSum=0) or (n>=High(A)) then
Abort;
//产生哑异常,让try..except捕获——最方便的跳出递归的方法 :)
end;
exit;
end;
n:=LeftCount;
while n>=0do
//寻找最高的起点
begin
if A[n]>LeftSum then
Dec(n)
else
break;
end;
for i:=ndo
wnto 0do
//贪心算法——先大后小
begin
Used:=true;
FindLeft(LeftSum-A,i-1);
//确保以后取的值比现在小
Used:=false;
end;
end;
begin
QuickSortIA(0,High(A));
SetLength(Used,High(A)+1);
FillChar(Used[0],(High(A)+1)*SizeOf(Boolean),0);
MinLeft:=Sum;
try
FindLeft(Sum,High(A));
except
end;
Result:=Sum-MinLeft;
end;
 
不知道你是干什么用的,所以也没测试,只保证语法没错误。你试试吧。
#include <stdlib.h>
#include <sysdyn.h>
//type
// IA=array of Integer;
typedef DynamicArray<int> IA;
typedef DynamicArray<bool> BA;
class Out{};
//function GenRandArray(MinNum,MaxNum,MinValue,MaxValue:Integer):IA;
//var
// i,n:Integer;
//begin
// Randomize;
// n:=MinNum+Random(MaxNum-MinNum+1);
// SetLength(Result,n);
// for i:=0 to n-1do
// Result:=MinValue+Random(MaxValue-MinValue+1);
//end;
IA GenRandArray(int MinNum, int MaxNum, int MinValue, int MaxValue)
{
int n;
IA Result;
randomize();
n = MinNum + random(MaxNum - MinNum + 1);
Result.Length = n;
for(int i = 0;
i < n;
i ++)
{
Result = MinValue + random(MaxValue - MinValue + 1);
}
return Result;
}
//function FindMaxSum(A:IA;Sum:Integer):Integer;
//var
// MinLeft:Integer;
// Used:array of Boolean;
// procedure QuickSortIA(L, R: Integer);
//从小到大排序
// var
// I, J, P, M: Integer;
// begin
// repeat
// I := L;
// J := R;
// P := (L + R) shr 1;
// repeat
// while A<A[P]do
Inc(I);
// while A[J]>A[P]do
Dec(J);
// if I <= J then
// begin
// M:=A;
// A:=A[J];
// A[J]:=M;
// if P = I then
// P := J
// else
if P = J then
// P := I;
// Inc(I);
// Dec(J);
// end;
// until I > J;
// if L < J then
QuickSortIA(L, J);
// L := I;
// until I >= R;
// end;
void QuickSortIA(IA &amp;A, int L, int R)
{
int I, J, P, M;
do
{
I = L;
J = R;
P = (L + R) >> 1;
do
{
while(A < A[P]) I ++;
while(A[J] > A[P]) J --;
if(I <= J)
{
M = A;
A = A[J];
A[J] = M;
if(P == I) P = J;
else
if(P == J) P = I;
I ++;
J --;
}
}while(I > J);
if(L < J) QuickSortIA(A, L, J);
L = I;
}while(I >= R);
}
// procedure FindLeft(LeftSum,LeftCount:Integer);
void FindLeft(IA &amp;A, int LeftSum, int LeftCount, BA &amp;Used, int &amp;MinLeft)
{
// var
// i,n,s:Integer;
// Str:String;
int n, s;
String Str;
// begin
// if (A[0]>LeftSum) or Used[0] then
// begin
// if LeftSum<MinLeft then
// begin
// MinLeft:=LeftSum;
// Str:='';
// s:=0;
// n:=0;
// for i:=0 to High(A)do
// if Used then
// begin
// Str:=Str+IntToStr(A)+' ';
// Inc(s,A);
// Inc(n);
// end;
// Str:=Str+#13#10+IntToStr(s);
// Form1.Memo1.Lines.Add(Str);
// Application.ProcessMessages;
//如果已经达到理想和值,或者已经耗尽了所有元素——该结束了
// if (LeftSum=0) or (n>=High(A)) then
// Abort;
//产生哑异常,让try..except捕获——最方便的跳出递归的方法 :)
// end;
// exit;
// end;
if(A[0] > LeftSum || Used[0])
{
if(LeftSum < MinLeft)
{
MinLeft = LeftSum;
Str = "";
s = n = 0;
for(int i = 0;
i < A.Length;
i ++)
if(Used)
{
Str += IntToStr(A) + " ";
s += A;
n ++;
}
Str += "/x0d/x0a" + IntToStr(s);
Form1->Memo1->Lines->Add(Str);
Application->ProcessMessages();
if(LeftSum == 0 || n >= A.Length - 1)
throw Out();
}
return;
}
// n:=LeftCount;
n = LeftCount;
// while n>=0do
//寻找最高的起点
// begin
// if A[n]>LeftSum then
// Dec(n)
// else
// break;
// end;
while(n >= 0)
{
if(A[n] > LeftSum)
n --;
else
break;
}
// for i:=ndo
wnto 0do
//贪心算法——先大后小
// begin
// Used:=true;
// FindLeft(LeftSum-A,i-1);
//确保以后取的值比现在小
// Used:=false;
// end;
for(int i = n;
i >= 0;
i --)
{
Used = true;
FindLeft(A, LeftSum - A, i - 1, Used, MinLeft);
Used = false;
}
// end;
}
int FindMaxSum(IA &amp;A, int Sum)
{
BA Used;
int MinLeft;
//begin
// QuickSortIA(0,High(A));
QuickSortIA(A, 0, A.Length - 1);
// SetLength(Used,High(A)+1);
Used.Length = A.Length;
// FillChar(Used[0],(High(A)+1)*SizeOf(Boolean),0);
for(int i = 0;
i < A.Length;
i ++)
Used = false;
// MinLeft:=Sum;
MinLeft = Sum;
// try
// FindLeft(Sum,High(A));
// except
// end;
try
{
FindLeft(A, Sum, A.Length - 1, Used, MinLeft);
}
catch(...)
{
}
// Result:=Sum-MinLeft;
return Sum - MinLeft;
//end;
}
 

Similar threads

I
回复
0
查看
671
import
I
I
回复
0
查看
588
import
I
I
回复
0
查看
540
import
I
I
回复
0
查看
500
import
I
顶部