unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;
type
TMyAAA = record
myface: integer;
myreverse: integer;
mydiff: integer;
end;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
StringGrid1: TStringGrid;
Label3: TLabel;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
private
function MYcheck(V: integer): boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function mytostr(V1, v2, v3, v4, oldcol, oldrow: integer): string;
begin
result := format('正%d 反%d|由Cells[%d, %d]翻开%d个正的和%d个反的', [V1, V2, oldcol, oldrow, V3, v4]);
end;
function mytoint(v: string): integer;
begin
result := strtoint(trim(copy(v, 3, pos(' ', v) - 2)));
end;
function TForm1.MYcheck(V: integer): boolean;
var
i: integer;
str: string;
begin
for i := 0 to StringGrid1.ColCount - 1do
begin
str := StringGrid1.Cells[i, v];
if str <> '' then
if mytoint(str) = 0 then
begin
result := false;
StringGrid1.Cells[i, v] := StringGrid1.Cells[i, v] + ' 解!';
showmessage(format('解cell[%d,%d]', [i, v]));
exit;
end;
end;
for i := 0 to StringGrid1.ColCount - 1do
begin
str := StringGrid1.Cells[i, v];
if str <> '' then
if mytoint(str) > 0 then
begin
result := true;
exit;
end;
end;
result := false;
showmessage('无解');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MyAAA: array of TMyAAA;
nowrow, MyAll, target, everyCount, i, m, j, tempmyface, tempmyreverse: integer;
procedure dd(V: integer);
var
i, j: integer;
myreverse, myface: integer;
begin
setlength(MyAAA, 1);
MyAAA[0].mydiff := v;
MyAAA[0].myface := v;
MyAAA[0].myreverse := 0;
for i := 1 to (v div 2)do
begin
myreverse := i;
myface := v - i;
if myface - myreverse > 0 then
begin
j := length(MyAAA);
setlength(MyAAA, j + 1);
MyAAA[j].mydiff := myface - myreverse;
MyAAA[j].myface := myface;
MyAAA[j].myreverse := myreverse;
end;
end;
end;
begin
MyAll := strtoint(edit1.Text);
target := strtoint(edit2.Text);
everyCount := strtoint(edit3.Text);
with StringGrid1do
begin
for i := 0 to rowcount - 1do
for j := 0 to colcount - 1do
Cells[j, i] := '';
rowcount := 1;
colcount := 1;
StringGrid1.Cells[0, 0] := format('正%d 反%d', [target, MyAll - target]);
end;
dd(everyCount);
nowrow := 0;
while MYcheck(nowrow)do
begin
j := 0;
for i := 0 to StringGrid1.ColCount - 1do
if StringGrid1.Cells[i, nowrow] <> '' then
begin
tempmyface := mytoint(StringGrid1.Cells[i, nowrow]);
tempmyreverse := MyAll - tempmyface;
for m := low(MyAAA) to high(MyAAA)do
if tempmyface >= MyAAA[m].myface then
if tempmyreverse >= MyAAA[m].myreverse then
begin
StringGrid1.Cells[j, nowrow + 1] := mytostr(tempmyface - MyAAA[m].mydiff, tempmyreverse + MyAAA[m].mydiff, MyAAA[m].myface, MyAAA[m].myreverse, i, nowrow);
j := j + 1;
end;
end;
if j > StringGrid1.ColCount then
StringGrid1.ColCount := j;
StringGrid1.RowCount := StringGrid1.RowCount + 1;
nowrow := nowrow + 1;
end;
end;
end.
object Form1: TForm1
Left = 63
Top = 146
Width = 795
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -14
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
PixelsPerInch = 96
TextHeight = 14
object Label1: TLabel
Left = 3
Top = 12
Width = 21
Height = 14
Caption = 'All'
end
object Label2: TLabel
Left = 3
Top = 36
Width = 14
Height = 14
Caption = '正'
end
object Label3: TLabel
Left = 3
Top = 60
Width = 28
Height = 14
Caption = '每次'
end
object Button1: TButton
Left = 184
Top = 8
Width = 75
Height = 25
Caption = '开始'
TabOrder = 0
OnClick = Button1Click
end
object Edit1: TEdit
Left = 32
Top = 8
Width = 121
Height = 22
TabOrder = 1
Text = '20'
end
object Edit2: TEdit
Left = 32
Top = 32
Width = 121
Height = 22
TabOrder = 2
Text = '13'
end
object StringGrid1: TStringGrid
Left = 0
Top = 88
Width = 787
Height = 260
Align = alBottom
DefaultColWidth = 280
DefaultRowHeight = 20
FixedCols = 0
FixedRows = 0
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
ParentFont = False
TabOrder = 3
end
object Edit3: TEdit
Left = 32
Top = 56
Width = 121
Height = 22
TabOrder = 4
Text = '5'
end
end