经典面试问题:12小球问题算法(源码)

<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog01.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>

(文档请参考:http://blog.csdn.net/CXXSoft/archive/2006/09/28/1299731.aspx)

3、运行效果

经典面试问题:12小球问题算法(源码)_第1张图片

4、 算法源码

... {
作品名称:小球问题通用解决方案
开发作者:成晓旭
开发时间:2003年01月22日
完成时间:2003年01月23日
修改时间1:2003年11月14日
增加用户问题条件设置绘制方法
修改时间2:2003年11月18日
增加比较过程的记录功能
}

unitCommon;

interface

uses
Windows,SysUtils,Classes,Graphics,BallType;

// 清除画面方法
procedureClearCanvas(aCanvas:TCanvas;aRect:TRect);
// 小球问题条件设置方法
procedureDraw_Ball_Config(
AllBall:arrayofTC_Ball;
ACanvas:TCanvas;
aClearRect:TRect;
bShowTrace:Boolean);
// 小球问题解决方法
procedureSerach_Error_Ball(
AllBall:arrayofTC_Ball;
ACanvas:TCanvas;
aClearRect:TRect;
bShowTrace:Boolean);
var
strLog1:AnsiString;
strLog2:AnsiString;
strLog3:AnsiString;


implementation
// 单元内部常量定义
const
Fir_Pivot_X
= 200 ;
Fir_Pivot_Y
= 80 ;
Hint_X
= 10 ;
One_DrawDelta
= 140 ;
One_PreDelta
= 70 ;
One_FroDelta
= 30 ;
strADyB
= ' 比较:A端(重)>B端(轻) ' + CHR( 13 ) + CHR( 10 );
strAXDB
= ' 比较:A端=B端 ' + CHR( 13 ) + CHR( 10 );
strAXyB
= ' 比较:A端(轻)'+CHR(13)+CHR(10);
A_Team
='A组:';
B_Team
='B组:';
preTail0
='号球'+CHR(13)+CHR(10);
preTail1
='号球';
proHead
='结论:异常球在[';
lastResult
='结论:异常球是';
nextHint
=CHR(13)+CHR(10)+'启示:';
ErrorHint
='命题不严密,请检查设置条件!';

functionSearchBall_At4(AllBall:arrayofTC_Ball;
A,G:arrayofByte;varvErr_Ball_Order:Byte;
varvIsHeavy:Boolean;ACanvas:TCanvas;bShowTrace:Boolean):Boolean;
var
A2,B2:Word;
A3,B3:Word;
Loop:Word;
bNumber:Byte;
bPartA,bPartB:arrayofTC_Ball;
bCmpPara:TC_CmpPara;
str:AnsiString;
begin
vErr_Ball_Order:
=0;
vIsHeavy:
=False;
A2:
=AllBall[A[1]].Weight+AllBall[A[2]].Weight+AllBall[G[1]].Weight;
B2:
=AllBall[A[3]].Weight+AllBall[G[2]].Weight+AllBall[G[3]].Weight;

str:
=A_Team+IntToStr(AllBall[A[1]].Order)+','
+IntToStr(AllBall[A[2]].Order)+','
+IntToStr(AllBall[G[1]].Order);
str:
=str+preTail0;
strLog2:
=strLog2+str;
str:
=B_Team+IntToStr(AllBall[A[3]].Order)+','
+IntToStr(AllBall[G[2]].Order)+','
+IntToStr(AllBall[G[3]].Order);
str:
=str+preTail0;
strLog2:
=strLog2+str;
bNumber:
=3;
SetLength(bPartA,bNumber);
SetLength(bPartB,bNumber);
bPartA[
0]:=AllBall[A[1]];
bPartA[
1]:=AllBall[A[2]];
bPartA[
2]:=AllBall[G[1]];

bPartB[
0]:=AllBall[A[3]];
bPartB[
1]:=AllBall[G[2]];
bPartB[
2]:=AllBall[G[3]];

Balance_One_Compare(Point(Fir_Pivot_X,Fir_Pivot_Y
+One_DrawDelta),
bNumber,bPartA,bPartB,ACanvas,bShowTrace);

ifA2=B2then
begin
A3:
=AllBall[A[4]].Weight;
B3:
=AllBall[G[1]].Weight;
strLog2:
=strLog2+strAXDB;
str:
=proHead;
str:
=str+IntToStr(AllBall[A[4]].Order);
str:
=str+']'+preTail1+'【排3余1】';
strLog2:
=strLog2+str;
str:
='用任一正常球与之比较,即可知异常球是偏轻偏重!';
strLog2:
=strLog2+nextHint+str;
withbCmpPara
do
begin
Pre_LNumber:
=4;
Fro_LNumber:
=1;
SetLength(Pre_Latency,Pre_LNumber);
SetLength(Fro_Latency,Fro_LNumber);
forLoop:=0toPre_LNumber-1do
Pre_Latency[Loop]:
=AllBall[Loop+9];
Fro_Latency[
0]:=AllBall[A[4]];
end;

Balance_One_Latency(Point(Hint_X,Fir_Pivot_Y
+One_DrawDelta-One_PreDelta),
Point(Hint_X,Fir_Pivot_Y
+One_DrawDelta+One_FroDelta),
bCmpPara,ACanvas,bShowTrace);
bNumber:
=1;
SetLength(bPartA,bNumber);
SetLength(bPartB,bNumber);
bPartA[
0]:=AllBall[A[4]];
bPartB[
0]:=AllBall[G[1]];

Balance_One_Compare(Point(Fir_Pivot_X,Fir_Pivot_Y
+One_DrawDelta*2),
bNumber,bPartA,bPartB,ACanvas,bShowTrace);

ifA3=B3then
begin
vErr_Ball_Order:
=0;
strLog3:
='异常球与正常球一样重!'+ErrorHint;
end
else
begin
vErr_Ball_Order:
=A[4];
vIsHeavy:
=A3>B3;
end;
end
else
begin
A3:
=AllBall[A[1]].Weight;
B3:
=AllBall[A[2]].Weight;
ifA2>B2then
strLog2:
=strLog2+strADYB
else
strLog2:
=strLog2+strAXYB;
str:
=proHead;
str:
=str+IntToStr(AllBall[A[1]].Order)+','
+IntToStr(AllBall[A[2]].Order)+','
+IntToStr(AllBall[A[3]].Order);
str:
=str+']'+preTail1+'【排1余3】';
strLog2:
=strLog2+str;
str:
='下一轮必须在本轮比较的同一端的两球中进行.即取:'
+IntToStr(AllBall[A[1]].Order)+','
+IntToStr(AllBall[A[2]].Order)
+'号球,在推算结果时,还必须用到此轮A、B端谁轻谁重!';
strLog2:
=strLog2+nextHint+str;
bNumber:
=1;
SetLength(bPartA,bNumber);
SetLength(bPartB,bNumber);
bPartA[
0]:=AllBall[A[1]];
bPartB[
0]:=AllBall[A[2]];

Balance_One_Compare(Point(Fir_Pivot_X,Fir_Pivot_Y
+One_DrawDelta*2),
bNumber,bPartA,bPartB,ACanvas,bShowTrace);

ifA3=B3then
begin
vErr_Ball_Order:
=A[3];
vIsHeavy:
=A2B2;
end
else
begin
ifA2>B2then
begin
ifA3>B3then
vErr_Ball_Order:
=A[1]
else
vErr_Ball_Order:
=A[2];
//IsHeavy:=True;
end
else
begin
ifA3>B3then
vErr_Ball_Order:
=A[2]
else
vErr_Ball_Order:
=A[1];
//IsHeavy:=NOTTrue;
end;
vIsHeavy:
=A2>B2;
end;
end;
Result:
=vErr_Ball_Order0;
end;


functionSearchBall_At8(AllBall:arrayofTC_Ball;IsAdyB:Boolean;
A,B,G:arrayofByte;varvErr_Ball_Order:Byte;
varvIsHeavy:Boolean;ACanvas:TCanvas;bShowTrace:Boolean):Boolean;
var
A2,B2:Word;
A3,B3:Word;
bNumber:Byte;
bPartA,bPartB:arrayofTC_Ball;
senPivot,thrPivot:TPoint;
str:AnsiString;
begin
vErr_Ball_Order:
=0;
vIsHeavy:
=False;
A2:
=AllBall[A[1]].Weight+AllBall[A[2]].Weight+AllBall[B[1]].Weight;
B2:
=AllBall[A[3]].Weight+AllBall[B[2]].Weight+AllBall[G[1]].Weight;

str:
=A_Team+IntToStr(AllBall[A[1]].Order)+','
+IntToStr(AllBall[A[2]].Order)+','
+IntToStr(AllBall[B[1]].Order);
str:
=str+preTail0;
strLog2:
=strLog2+str;
str:
=B_Team+IntToStr(AllBall[A[3]].Order)+','
+IntToStr(AllBall[B[2]].Order)+','
+IntToStr(AllBall[G[1]].Order);
str:
=str+preTail0;
strLog2:
=strLog2+str;
bNumber:
=3;
SetLength(bPartA,bNumber);
SetLength(bPartB,bNumber);
bPartA[
0]:=AllBall[A[1]];
bPartA[
1]:=AllBall[A[2]];
bPartA[
2]:=AllBall[B[1]];

bPartB[
0]:=AllBall[A[3]];
bPartB[
1]:=AllBall[B[2]];
bPartB[
2]:=AllBall[G[1]];

Balance_One_Compare(Point(Fir_Pivot_X,Fir_Pivot_Y
+One_DrawDelta),
bNumber,bPartA,bPartB,ACanvas,bShowTrace);
ifA2=B2then
begin
A3:
=AllBall[B[3]].Weight;
B3:
=AllBall[B[4]].Weight;
strLog2:
=strLog2+strAXDB;
str:
=proHead;
str:
=str+IntToStr(AllBall[A[4]].Order)+','
+IntToStr(AllBall[B[3]].Order)+','
+IntToStr(AllBall[B[4]].Order);
str:
=str+']'+preTail1+'【排5余3】';
strLog2:
=strLog2+str;
str:</span
分享到:
评论
happmaoo
  • 浏览: 1288293 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

你可能感兴趣的:(算法,windows,面试,Blog)