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 ;
}
 

poj 1348 Computing (四个数的加减乘除四则运算)的更多相关文章

  1. Qt之加减乘除四则运算-支持负数

    一.效果展示 如图1所示,是简单的四则运算测试效果,第一列为原始表达式,第二列为转换后的后缀表达式,冒号后为结果.表达式支持负数和空格,图中是使用了5组测试数据,测试结果可能不全,如大家发现算法有问题 ...

  2. java课后作业 弹出窗口求两个数的加减乘除

    //计算2个数的加减乘除 谷伟华 2015/10/6package jisuan; import javax.swing.JOptionPane; public class Jiasuan { pub ...

  3. lintcode:四个数之和

    题目 四数之和 给一个包含n个数的整数数组S,在S中找到所有使得和为给定整数target的四元组(a, b, c, d). 样例 例如,对于给定的整数数组S=. 满足要求的四元组集合为: (-1, 0 ...

  4. js jq 手机号实现(344) 附带删除功能 jq 实现银行卡没四个数加一个空格 附带删除功能

    js 手机号实现(344)  下面有将正则验证去掉“-” 或“空格”  下一篇博客有单独的删除功能方法 <!DOCTYPE html> <head> <meta char ...

  5. Python基础算法综合:加减乘除四则运算方法

    #!usr/bin/env python# -*- coding:utf-8 -*-#python的算法加减乘除用符号:+,-,*,/来表示#以下全是python2.x写法,3.x以上请在python ...

  6. java实现超大整数加减乘除四则运算

    原理: 用数组存储数字,按照计算法则进行运算. 代码: package com.hdwang; import java.util.regex.Matcher; import java.util.reg ...

  7. poj 1144(求割点个数)

    题目链接:http://poj.org/problem?id=1144 思路:判断一个点是否是割点的两个条件:1.如果一个点v是根结点并且它的子女个数大于等于2,则v是割点.2.如果点v不是根结点,并 ...

  8. Java位运算实现加减乘除四则运算

    本文是继<一文了解有趣的位运算>的第二篇文章. 我们知道,计算机最基本的操作单元是字节(byte),一个字节由8个位(bit)组成,一个位只能存储一个0或1,其实也就是高低电平.无论多么复 ...

  9. 位运算实现加减乘除四则运算(Java)

    [本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究.若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!] 本文是继< ...

随机推荐

  1. 使用RecyclerView实现瀑布流的效果

    主函数: public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; privat ...

  2. Android 自学之绝对布局 AbsoluteLayout

    绝对布局(AbsoluteLayout),绝对布局就像java AWT中的空布局:所谓的绝对布局就是Android不提供任何的布局控制,而是有开发人员自己通过X坐标和Y坐标来控制组件的位置.当使用绝对 ...

  3. Redis命令参考(Keys & String)

    r = redis.Redis(ip, port, index) 如此实例化一个redis对象,index取值0-15,一个redis对象有16个库. Keys 函数 功能 返回值 备注 keys(s ...

  4. 输入参数varargin

    一种特别的输入参数varargin 可以在自定义函数中得到,这种函数支持输入参数的变量的个数.这个参数显在输入参数列表的最后一项,它返回一个单元阵列,所以一个输入实参可以包括任意数目的实参.每一个实参 ...

  5. Oracle性能调优(AWR)

    一.AWR报告 AWR 是通过对比两次快照(snapshot)收集到的统计信息,来生成报表数据,生成的报表包括多个部分,这点与Statspack生成的报告非常类似.不过AWR在生成报告时,可以选择生成 ...

  6. PHP 有关上传图片时返回HTTP 500错误

    1. 检查PHP GD 扩展库是否开启或者安装 在Ubuntu server中,在php -m 之后,未看到gd扩展,所以需要安装gd,然后重启apache2 sudo apt-get install ...

  7. 【高级JEE技术】JMX

    JMX即Java Manager Extentin(java 管理扩展)一种动态改变javabean属性值的技术,具体应用场景可以有很多.比如使用JMX作为线上应用的开关,在做一些新老系统改造的时候 ...

  8. SQL 分组后取最小行号记录

    本示例测试两个表联接查询后,分组并取分组后的最小行号记录 测试表: tb1表结构如下: CREATE TABLE [dbo].[tb1]( ) NOT NULL, ) NULL, ) NULL, CO ...

  9. and or判别

    and 和 or涉及到短路运算,python把0,'',none看做false,其余是true, 对于a and b,若a是true则,返回b,若a是false则返回a(因为a是true还需要判断b, ...

  10. 浅析foreach原理

    在日常开发工作中,我们发现很多对象都能通过foreach来遍历,比如HashTable.Dictionary.数组等数据类型.那为何这些对象能通过foreach来遍历呢?如果写一个普通的Person类 ...