Показать сообщение отдельно
Старый 29.06.2004, 13:29     # 7
SwiMMeR
::VIP::
Отыпный Саводод
 
Аватар для SwiMMeR
 
Регистрация: 27.10.2002
Адрес: Краснодар, Россия
Пол: Male
Сообщения: 452

SwiMMeR Имеются все основания чтобы гордиться собойSwiMMeR Имеются все основания чтобы гордиться собойSwiMMeR Имеются все основания чтобы гордиться собойSwiMMeR Имеются все основания чтобы гордиться собойSwiMMeR Имеются все основания чтобы гордиться собойSwiMMeR Имеются все основания чтобы гордиться собойSwiMMeR Имеются все основания чтобы гордиться собойSwiMMeR Имеются все основания чтобы гордиться собойSwiMMeR Имеются все основания чтобы гордиться собой
Pascal последний раз юзал лет 6-7 назад, поэтому не судите строго
Код:
uses crt;

 const n=5;
 const nn=32; {nn=2^n}

 function stepen(x,y : integer) : integer;
 begin
  if y=0 then
   stepen:=1
  else
   if y=1 then
    stepen:=x
   else
    begin
     y:=y-1;
     stepen:=x*stepen(x,y);
    end;
 end;

 procedure save(var res : array of string; a : array of integer);
 var max_i, ti : integer;
            zz : string;
 begin
  max_i:=0;
  for ti:=0 to stepen(2,n)-1 do
   if (length(res[ti])=0) and (max_i=0) then
    max_i:=ti;
  res[max_i]:='{';
  for ti:=0 to n-1 do
   if a[ti]=1 then
    begin
     if (ti>0) and (length(res[max_i])>1) then
      res[max_i]:=res[max_i]+',';
     str(ti+1,zz);
     res[max_i]:=res[max_i]+zz;
    end;
  res[max_i]:=res[max_i]+'}';
 end;

 procedure summ(sum,tsum,ti : integer; var count : integer;x:array of integer;var a:array of integer;var res:array of string);
 var ttsum, si : integer;
            zz : string;
 begin
  ttsum:=tsum+x[ti];
  if ttsum<sum then
   for si:=ti+1 to n-1 do
    begin
     a[si]:=1;
     summ(sum,ttsum,si,count,x,a,res);
     a[si]:=0;
    end
  else
   if ttsum=sum then
    begin
     count:=count+1;
     save(res,a);
    end
   else
    a[ti]:=0;
 end;

 var sum, tsum, i, count : integer;
                      zz : string;
                    x, a : array[0..n-1] of integer;
                     res : array[0..nn-1] of string;
begin
 clrscr;
 x[0]:=1; x[1]:=2; x[2]:=2; x[3]:=3; x[4]:=3;
 sum:=6;
 count:=0;
 a[0]:=0; a[1]:=0; a[2]:=0; a[3]:=0; a[4]:=0;
 tsum:=0;
 if tsum=sum then
  begin
   count:=count+1;
   save(res,a);
  end;
 for i:=0 to n-1 do
  begin
   a[i]:=1;
   summ(sum,tsum,i,count,x,a,res);
   a[0]:=0; a[1]:=0; a[2]:=0; a[3]:=0; a[4]:=0;
  end;
 writeln;
 str(count,zz);
 writeln('Count of SubArrays = '+zz);
 i:=1;
 if count>0 then
  writeln;
 for i:=1 to count do
  begin
   writeln(res[i]);
  end;
 readkey;
end.
В архиве сам файл
Вложения
Тип файла: rar TEST.rar (640 байт, 30 просмотров - Кто скачивал? )
__________________
The Information will be FREE!

Последний раз редактировалось SwiMMeR; 29.06.2004 в 13:41.
SwiMMeR вне форума