kiem tien, kiem tien online, kiem tien truc tuyen, kiem tien tren mang
Wednesday, October 19, 2011

Bài 5: In ra hoán vị của một dãy i = 1....n;
//Code implemantation:


//10_20_2011_bai5.cpp
#include"iostream"
using namespace std;

int *X,n, OK =1, sum = 0;

void Init()
{
cout<<"\Nhap n =";cin>>n;
X= new int [n];
for (int i=1;i<=n;i++)
X[i] = i;
}

void Result()
{
cout<<"\nKet qua " << ++sum << " la: ";
for( int i =1; i<=n;i++)
cout<< X[i] << " ";
}

void Next_Permutation()
{
int j = n-1;
while( j>0 && X[j] > X[j+1]) j--;

if(j>0)
{
int k = n;
while(X[j] > X[k]) k--;

// Hoan vi X[j] va X[k]

int t = X[j]; X[j] = X[k]; X[k] = t;
int r = j+1, s =n;
while( r <=s)
{
// Hoan vi X[r] va X[s]
t = X[r];X[r] = X[s];X[s] = t;
r++; s--;
}
}

else OK = 0;
}

int main()
{
Init();
while(OK)
{
Result();
Next_Permutation();
}
cout<< endl;
//delete(X);

system("pause");
return 0;
}



0 comments:

Post a Comment

domain, domain name, premium domain name for sales

Popular Posts