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. PHP Slim 框架初体验之无法访问控制器

    话不多说,先把报错贴出来: 刚开始用slim框架,在设置完自动加载文件和路由文件之后,我写了一个控制器: <?php use \Psr\Http\Message\ServerRequestInt ...

  2. httperf ---linux web站点压力测试

    一.工具下载&&安装 软件获取 ftp://ftp.hpl.hp.com/pub/httperf/ 这里使用的是如下的版本 ftp://ftp.hpl.hp.com/pub/httpe ...

  3. 这五个题你懂了javascript你就入门了

    1. if (!("a" in window)) { var a = 1; } alert(a); 阅读代码:如果window不包含属性a,就声明一个变量a,然后赋值为1,最后弹出 ...

  4. SpringMVC使用@ResponseBody注解返回中文字符串乱码的问题

    先说一下我的经历,以及解决问题的而过程. 在使用SpringMVC的时候,最开始的时候在配置文件中使用<mvc:annotation-driven />去自动注册DefaultAnnota ...

  5. Linux VIM python 自动补全插件:pydiction

    Pydiction 可以是我们使用Tab键自动补全Python代码在Vim,是一款非常不错的插件. Pydiction不需要安装,所有没有任何依赖包问题,Pydiction主要包含三个文件. pyth ...

  6. casperjs 抓取爱奇艺高清视频

    CasperJS 是一个开源的导航脚本和测试工具,使用 JavaScript 基于 PhantomJS 编写,用于测试 Web 应用功能,Phantom JS是一个服务器端的 JavaScript A ...

  7. Java中内存空间的分配及回收

    Java中内存分为: 栈:存放简单数据类型变量(值和变量名都存在栈中),存放引用数据类型的变量名以及它所指向的实例的首地址. 堆:存放引用数据类型的实例. Java的垃圾回收: 由一个后台线程GC(G ...

  8. ~/.ssh目录找不到解决方法

    执行 cd ~/.ssh发现ssh目录找不到 [root@ocdp2 ~]# cd ~/.ssh -bash: cd: /root/.ssh: No such file or directory 原因 ...

  9. 读取tomcat下的文件夹路径

    背景:测试的为了每次部署时清缓存,将temp文件夹也删了,导致系统中有些excel导出功能用不了. 解决:新建一个监听文件,在系统启动时,判断temp文件夹是否存在,不存在就新建. temp文件夹的作 ...

  10. Oracle Split Partitions

    1. 创建分离分区的存储过程 CREATE OR REPLACE Procedure SP_Split_Partition( v_table_name_in in varchar2, v_part_n ...