Sun Sep 02, 2012 8:59 pm
Hưng Cho số nguyên dương n . Hãy tìm các cách p/tích số n = tổng các số nguyên dương . các cách p/tích là hoán vị chỉ tính là 1 :
vd : 6= 1 + 2 + 3
6= 1 + 5
6= 2 + 4
vd : 6= 1 + 2 + 3
6= 1 + 5
6= 2 + 4
- Code:
program Bai1;
Var t,x:array[0..100] Of Integer;
max,i,n:Integer;
Procedure nhap;
Var i:Integer;
Begin
Writeln(' nhap ');
Readln(n);
End;
Procedure Int;
Begin
t[0]:=0;max:=-maxint;
x[0]:=0;
End;
Procedure xuat(k:Integer);
Var d,i:Integer;
Begin
Write(' ',n,'=');
For i:=1 to k-1 do
Write(' ',x[i],'+');
Writeln(' ',x[k]);
End;
Procedure try(i:Integer);
Var j:Integer;
Begin
For j:=(x[i-1]+1) to n do
Begin
x[i]:=j;
t[i]:=t[i-1]+j;
If t[i]=n then xuat(i)
Else
try(i+1);
x[i]:=0;
End;
End;
Begin
nhap;
Int;
try(1);
Readln;
End.