poj 1348 Computing (四个数的加减乘除四则运算)

时间:2022-02-07 18:45:04

http://poj.org/problem?id=1348

Computing
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1681   Accepted: 248

Description

Input any five positive integral numbers n1, n2, n3, n4, n5, such that 0<=ni<=100, 1<=i<=5. To the first four positive integral numbers (n1, n2, n3, n4) the arithmetic operation, such as addition (+), subtraction (-), multiplication (*), or division (/) and brackets ('(',')') may be freely applied, but in the arithmetic expression formed with these numbers and operations, every one of the four integral numbers should be used once and only once.  Write a program for finding an arithmetic expression that satisfies the above requirement and equals n5.

Input

The input file consists of a number of data sets.Each data set is a line of 5 numbers separated by blank.A line of a single -1 represents the end of input.

Output

For each data set output the original data set first.If the program finds out the expression for these four arbitrary input numbers, then it gives out the output "OK!";On the contrary, if the program could not get the result of n5 by any arithmetic operations to the four input numbers, it gives output "NO!".

Sample Input

1 2 3 4 50
2 3 10 1 61
-1

Sample Output

1 2 3 4 50 NO!
2 3 10 1 61 OK!

Source

 
思路:
采用 分子分母 表示一个整数,进行四则运算;
利用STL中algorithm中的next_permutation(a,a+n)获取下一个字典序
 
代码:
 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm> using namespace std; struct Nod
{
int son; //分子
int mon; //分母
}num[]; //以 分子/分母 形式保存一个数 void getNum(int *a) //将a数组转换成 分子/分母 形式
{
int i;
for(i=;i<;i++)
{
num[i].son = a[i];
num[i].mon = ;
}
} Nod operate(Nod a,Nod b,int ch) //进行四则运算
{
Nod temp;
if(ch==) // '+'
{
temp.mon = a.mon * b.mon;
temp.son = a.son * b.mon + b.son * a.mon;
}
else if(ch==) // '-'
{
temp.mon = a.mon * b.mon;
temp.son = a.son * b.mon - b.son * a.mon;
}
else if(ch==) // '*'
{
temp.mon = a.mon * b.mon;
temp.son = a.son * b.son;
}
else if(ch==) // '/'
{
temp.mon = a.mon * b.son;
temp.son = b.mon * a.son;
}
return temp;
} int computing(int *a,int e)
{
getNum(a); //获得 分子/分母 的表示方式
Nod temp1,temp2,temp3;
int i,j,k; // ((a#b)#c)#d '#'号代表运算符号
for(i=;i<;i++)
{
temp1 = operate(num[],num[],i);
if(temp1.mon == ) continue; //分母为0情况
for(j=;j<;j++)
{
temp2 = operate(temp1,num[],j);
if(temp2.mon == ) continue;
for(k=;k<;k++)
{
temp3 = operate(temp2,num[],k);
if(temp3.mon == ) continue;
if(temp3.son%temp3.mon==&&temp3.son/temp3.mon==e) return ;
}
}
} //(a#(b#(c#d)))
for(i=;i<;i++)
{
temp1 = operate(num[],num[],i);
if(temp1.mon == ) continue;
for(j=;j<;j++)
{
temp2 = operate(num[],temp1,j);
if(temp2.mon == ) continue;
for(k=;k<;k++)
{
temp3 = operate(num[],temp2,k);
if(temp3.mon == ) continue;
if(temp3.son%temp3.mon==&&temp3.son/temp3.mon==e) return ;
}
}
}
//(a#b)#(c#d)
for(i=;i<;i++)
{
temp1 = operate(num[],num[],i);
if(temp1.mon == ) continue;
for(j=;j<;j++)
{
temp2 = operate(num[],num[],j);
if(temp2.mon == ) continue;
for(k=;k<;k++)
{
temp3 = operate(temp1,temp2,k);
if(temp3.mon == ) continue;
if(temp3.son%temp3.mon==&&temp3.son/temp3.mon==e) return ;
}
}
}
return ;
} int main()
{
int a[],e;
while(~scanf("%d",&a[])&&a[]!=-)
{
scanf("%d%d%d%d",&a[],&a[],&a[],&e);
int i,j;
for(j=;j<;j++) printf("%d ",a[j]);
for(i=;i<;i++)
{
if(computing(a,e) == ) break;
next_permutation(a,a+); //获取下一个字典序
}
printf("%d ",e);
if(i<) puts("OK!");
else puts("NO!");
}
return ;
}