procedure TForm1.Button1Click(Sender: TObject);
const
cBaseNumbers: array[0..16] of Integer = (858, 300, 240, 120, 90, 85, 60, 30,
25, 21, 17, 12, 8, 7, 6, 5, 4);
var
vMinCount: Integer;
vPath: string;
procedure pScan(
ADest: Integer;
ALevel: Integer;
ACount: Integer;
APath: string
);
var
I: Integer;
begin
if ALevel > High(cBaseNumbers) then Exit;
if ADest = 0 then
begin
if ACount < vMinCount then
begin
vMinCount := ACount;
vPath := APath;
end;
end
else
begin
I := ALevel;
while I <= High(cBaseNumbers) do
begin
if ADest >= cBaseNumbers then
pScan(ADest mod cBaseNumbers, ALevel + 1,
ACount + ADest div cBaseNumbers,
APath + Format('+%d*%d', [ADest div cBaseNumbers,
cBaseNumbers]));
Inc(I);
end;
end;
end;
class Count
{
public int[] BaseNumbers = new int[] {858,300,240,120,90,85,60,
30,25,21,17,12,8,7,6,5,4};
public string[] NumberStrings = new string[] { "858","300","240","120","90","85","60","30","25","21","17","12","8","7","6","5","4"};
public StringBuilder strb = new StringBuilder("目标数分解为:\r\n\r\n", 64);
public void Scan(int target) {
for (int i = 0; i < BaseNumbers.Length; i++)
{
if (target >= BaseNumbers) {
int level = (int)(target/BaseNumbers);
target = target % BaseNumbers;
strb.AppendFormat("{0}次{1}\r\n", level.ToString(), NumberStrings);
版主的代码我还是很难理解,而且迭代的次数非常之多,感觉比较影响效率
我终于想到了比较简单而且思路清晰的函数实现,只需在原来的基础上加上一条判断即可,真是恍然大悟啊
class Count
{
public int[] BaseNumbers = new int[] {858,300,240,120,90,85,60,
30,25,21,17,12,8,7,6,5,4};
public string[] NumberStrings = new string[] { "858","300","240","120","90","85","60","30","25","21","17","12","8","7","6","5","4"};
public StringBuilder strb = new StringBuilder("目标数分解为:\r\n\r\n", 64);
public void Scan(int target) {
for (int i = 0; i < BaseNumbers.Length; i++)
{
if (target >= BaseNumbers && (target%BaseNumbers[i]>4 || target%BaseNumbers[i]==0)) {
int level = (int)(target/BaseNumbers);
target = target % BaseNumbers;
strb.AppendFormat("{0}次{1}\r\n", level.ToString(), NumberStrings);