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. 关于oozie

    什么是Oozie? Oozie是一个工作流引擎服务器,用于运行Hadoop Map/Reduce和Pig 任务工作流.同时Oozie还是一个Java Web程序,运行在Java Servlet容器中, ...

  2. ADC及DA的头文件复析

    /************************************************************* ADC12,,,,这么多的定义,搞得我都昏死啦,抽出来可能好几一些..** ...

  3. uiautomator的坑和AAPT命令方式启动一个应用程序

    最近在使用UIautomator完成公司的一个主流程的自动化,因为不适用H5和IOS所以会放弃这个工具的使用,现在记录在使用uiautomator的一些问题: 案列1:使用命令去启动要运用的apk包 ...

  4. Singleton(单例模式)

    类的实例化次数只能一次. 例如:小王和小李通过门铃进行沟通,首先判断小王家是否有门,若没有建立门,若有门直接返回门. var xiaowang = (function(argument){ var m ...

  5. union判断CPU是little-endian还是big-endian存储

    利用联合体的特殊存储方式,便能检测出来cpu所使用的事big-endian或者是little-endian的存储方式. 1.判断系统是Big liden 或者是little  ledin 方法1:in ...

  6. 40个Java集合面试问题和答案【中】【转载】

    接上文:http://www.cnblogs.com/xujianbo/p/5148075.html   16.UnsupportedOperationException是什么? Unsupporte ...

  7. ios 单例模式(懒汉式)

    1. 单例模式的作用 可以保证在程序运行过程,一个类只有一个实例,而且该实例易于供外界访问 从而方便地控制了实例个数,并节约系统资源 2. 单例模式的使用场合 在整个应用程序中,共享一份资源(这份资源 ...

  8. Walkthrough: Creating and Using a Dynamic Link Library (C++)

    Original Link: http://msdn.microsoft.com/zh-cn/library/ms235636.aspx Following content is only used ...

  9. bat文件的妙用1-一键开启所有开发软件

    每天早上来的第一件事情,就是打开电脑,然后开一堆的软件 1.wamp 开发环境 2.钉钉   通讯工具 3.PHPstrom 开发工具 4.nodejs.bat Nodejs的扩展(node D:/w ...

  10. ubuntu获取硬盘的uuid。

    1.用UUID来标识硬盘有很多好处,它是一个硬盘的唯一代号,所以当硬盘插口位置变化时,虽然sda可能会变成sdc,但这个码是不会变的.所以在 fstab中用/dev/sda1这样的硬盘标识可能会有混乱 ...