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. Android_gridView_LIstener_examle

    layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...

  2. 最佳vim技巧

    最佳vim技巧----------------------------------------# 信息来源----------------------------------------www.vim ...

  3. saltstack实战2--远程执行之返回(returner)

    saltstack有3大功能:远程执行,配置管理,云管理 其中远程执行又可分解为:目标,模块,返回  这3个部分. 比如下面语句 [root@master ~]# salt '*' test.ping ...

  4. videojs 动态加载视频

    VideoJS dynamic source change via RESTful API 'Undefined' issue with changing RTMP source on compres ...

  5. 将系统日期转化为sharepoint日期

    string currentDate = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now.Date);

  6. linux时间自动同步

    1,修正本地时区及ntp服务 #yum -y install ntp#rm -rf /etc/localtime#ln -s /usr/share/zoneinfo/Asia/Shanghai /et ...

  7. ExtJs store加载

    当store加载数据对象中,如果带有success:false,store会认为加载数据失败,不予将得到的数据显示在界面上,所有记得在返回数据对象的同时返回success:true;

  8. asp.net 中excel 导入数据库

    protected void Button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(Sy ...

  9. VS2013 右键监视,未能计算表达式的值

    工具>选项>调试>常规:将红框中的勾上,重新运行就可以了. https://weblog.west-wind.com/posts/2013/Nov/21/Visual-Studio- ...

  10. 日志记录类LogHelper

    开源日志log4net使用起来很方便,但是项目中不让用,所以自己重写了一个类,用来记录日志,比较简单. 1.首先是可以把日志分成多个类型,分别记录到不同的文件中 /// <summary> ...