Дано множество чисел и некоторое число. Можно ли из множества выбрать два элемента, которые в сумме дают заданное число — Delphi(Делфи)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Spin;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    SpinEdit1: TSpinEdit;
    Label1: TLabel;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  // const mn=[1,3,5,7,9,11,13,15];

var
  Form1: TForm1;
  a, i, j, n, k, g: byte;
  b: array [1 .. 50] of byte;
  mn: set of byte;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Clear();
  n := 0;
  mn := [];
  for g := 1 to 10 do
    include(mn, StrToInt(Edit1.text));
  for i := 0 to 255 do
    if i in mn then
    begin
      n := n + 1;
      b[n] := i;
      Memo1.Lines.Add(IntToStr(b[n]));
    end;
  a := SpinEdit1.Value;
  k := 0;
  for i := 1 to n - 1 do
    for j := i + 1 to n do
      if (i <> j) and (b[i] + b[j] = a) then
      begin
        k := k + 1;
        Memo1.Lines.Add('-----------');
        Memo1.Lines.Add(IntToStr(b[i]) + '+' + IntToStr(b[j]));
      end;
  if k = 0 then
    Memo1.Lines.Add('There are no pairs');
end;

end.

Leave a Comment