kiem tien, kiem tien online, kiem tien truc tuyen, kiem tien tren mang
Sunday, October 23, 2011

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

//Mang X[] de luu 4 gia tri 1,2,3,4 tuong ung voi *,/,+,-
//Mang A[] de luu 5 gia tri cua bieu thuc duoc nhap tu ban phim
//K la gia tri cua bieu thuc
//OK kiem tra tinh dung cua ham sinh hoan vi Next_Permunation

int X[4],A[5],n = 4,OK = 1,K ;

void Init()
{
cout<<"Nhap gia tri cua bieu thuc: ";
cin>>K;
for(int i=1;i<=5;i++)
{
cout<< "Nhap so "; cin >> A[i];
}
for(int i =1;i<=4;i++)
X[i] = i;
}

void Next_Permutation()
{
int j = n-1;
// Tim X[j] dau tien tu phai sang trai ma X[j] > X[j+1]
while( j> 0&& X[j] > X[j+1]) j--;

//Tim X[k] dau tien tu phai qua trai ma X[j] > X[k]
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]

// lat nguoc day a[j+1],a[j+2],a[n];
t = X[r]; X[r] = X[s]; X[s] = t;
++r; --s;
}
}
else OK = 0;
}

// In ra ket qua la thu tu cua 4 phep tinh
void result()
{

for(int i =1; i<=4;i++)

switch(X[i])
{
case 1: {cout<<"*"; break;}
case 2: { cout<<"/";break;}
case 3: {cout<<"+";break;}
case 4: {cout<<"-";break;}
}
}

int test()
{
int t[5];
for(int i=1;i<=5;i++)
{
t[i] = A[i];
}
// Bien bieu thuc co 4 phep tinh tro thanh bieu thuc chi con 2 phep tinh cong, tru
for ( int i = 1; i<=4; i++)
{
if( X[i]== 1)
{
t[i] = A[i]*A[i+1];
t[i+1] = 0;
if(i<4&&X[i+1] == 2)
{
t[i] = t[i]/A[i+2];
t[i+2]=0;
break;
}
}
if( X[i] == 2)
{
t[i] = A[i] / A[i+1];
t[i+1] = 0;
if( i<4 && X[i+1] == 1)
{
t[i] = t[i]/A[i+2];
t[i+2] = 0;
break;
}
}
}


int S = t[1];// S de luu gia tri cua bieu thuc
for( int i =2;i<=5;i++)
{
if(X[i-1] == 4) S-=t[i];
else S+=t[i];
}
if(S == K)return 1;
return 0;
}
int main()
{


Init();
while(OK)
{
if(test())result();

Next_Permutation();

}

//system("pause");
//return 0;
}






0 comments:

Post a Comment

domain, domain name, premium domain name for sales

Popular Posts