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. 1 2 3 4 50
  2. 2 3 10 1 61
  3. -1

Sample Output

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

Source

 
思路:
采用 分子分母 表示一个整数,进行四则运算;
利用STL中algorithm中的next_permutation(a,a+n)获取下一个字典序
 
代码:
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<algorithm>
  5.  
  6. using namespace std;
  7.  
  8. struct Nod
  9. {
  10. int son; //分子
  11. int mon; //分母
  12. }num[]; //以 分子/分母 形式保存一个数
  13.  
  14. void getNum(int *a) //将a数组转换成 分子/分母 形式
  15. {
  16. int i;
  17. for(i=;i<;i++)
  18. {
  19. num[i].son = a[i];
  20. num[i].mon = ;
  21. }
  22. }
  23.  
  24. Nod operate(Nod a,Nod b,int ch) //进行四则运算
  25. {
  26. Nod temp;
  27. if(ch==) // '+'
  28. {
  29. temp.mon = a.mon * b.mon;
  30. temp.son = a.son * b.mon + b.son * a.mon;
  31. }
  32. else if(ch==) // '-'
  33. {
  34. temp.mon = a.mon * b.mon;
  35. temp.son = a.son * b.mon - b.son * a.mon;
  36. }
  37. else if(ch==) // '*'
  38. {
  39. temp.mon = a.mon * b.mon;
  40. temp.son = a.son * b.son;
  41. }
  42. else if(ch==) // '/'
  43. {
  44. temp.mon = a.mon * b.son;
  45. temp.son = b.mon * a.son;
  46. }
  47. return temp;
  48. }
  49.  
  50. int computing(int *a,int e)
  51. {
  52. getNum(a); //获得 分子/分母 的表示方式
  53. Nod temp1,temp2,temp3;
  54. int i,j,k;
  55.  
  56. // ((a#b)#c)#d '#'号代表运算符号
  57. for(i=;i<;i++)
  58. {
  59. temp1 = operate(num[],num[],i);
  60. if(temp1.mon == ) continue; //分母为0情况
  61. for(j=;j<;j++)
  62. {
  63. temp2 = operate(temp1,num[],j);
  64. if(temp2.mon == ) continue;
  65. for(k=;k<;k++)
  66. {
  67. temp3 = operate(temp2,num[],k);
  68. if(temp3.mon == ) continue;
  69. if(temp3.son%temp3.mon==&&temp3.son/temp3.mon==e) return ;
  70. }
  71. }
  72. }
  73.  
  74. //(a#(b#(c#d)))
  75. for(i=;i<;i++)
  76. {
  77. temp1 = operate(num[],num[],i);
  78. if(temp1.mon == ) continue;
  79. for(j=;j<;j++)
  80. {
  81. temp2 = operate(num[],temp1,j);
  82. if(temp2.mon == ) continue;
  83. for(k=;k<;k++)
  84. {
  85. temp3 = operate(num[],temp2,k);
  86. if(temp3.mon == ) continue;
  87. if(temp3.son%temp3.mon==&&temp3.son/temp3.mon==e) return ;
  88. }
  89. }
  90. }
  91. //(a#b)#(c#d)
  92. for(i=;i<;i++)
  93. {
  94. temp1 = operate(num[],num[],i);
  95. if(temp1.mon == ) continue;
  96. for(j=;j<;j++)
  97. {
  98. temp2 = operate(num[],num[],j);
  99. if(temp2.mon == ) continue;
  100. for(k=;k<;k++)
  101. {
  102. temp3 = operate(temp1,temp2,k);
  103. if(temp3.mon == ) continue;
  104. if(temp3.son%temp3.mon==&&temp3.son/temp3.mon==e) return ;
  105. }
  106. }
  107. }
  108. return ;
  109. }
  110.  
  111. int main()
  112. {
  113. int a[],e;
  114. while(~scanf("%d",&a[])&&a[]!=-)
  115. {
  116. scanf("%d%d%d%d",&a[],&a[],&a[],&e);
  117. int i,j;
  118. for(j=;j<;j++) printf("%d ",a[j]);
  119. for(i=;i<;i++)
  120. {
  121. if(computing(a,e) == ) break;
  122. next_permutation(a,a+); //获取下一个字典序
  123. }
  124. printf("%d ",e);
  125. if(i<) puts("OK!");
  126. else puts("NO!");
  127. }
  128. return ;
  129. }
 

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. Forwarding a Range of Ports in VirtualBox

    STAN SCHWERTLY MAY 9, 2012 ARTICLES 3 COMMENTS Doesn't allow forwarding a range of ports through the ...

  2. opencv拼接相关1

    这里面都是一些比较杂的东西,没什么实际意义.主要是为了,后面能跑一个程序: Stitcher: 抠细节: http://docs.opencv.org/2.4.2/modules/stitching/ ...

  3. C语言里的文件函数

    1.File *pf = fopen("文件名","打开方式"); 文件名可以加路径,两个"\\"或者一个"/" 打开方 ...

  4. 介绍一些适用于 Web 开发者的 Atom 编辑器插件

    Atom 的社区很繁荣,有着丰富的扩展/插件(packages).安装 Atom 的 Package 非常简单,可以在编辑器的偏好设置里面安装,也可以在命令行中使用 apm 命令来安装. 在介绍适用于 ...

  5. ssh 安全配置

    1.只使用ssh v2  //etc/ssh/sshd_cofig Protocol 2 ListenAddress x.x.x.x --如果你的环境有VPN通道,建议sshd监听所在的内网地址: 2 ...

  6. uiatuomator提示shortMsg=java.lang.RuntimeException

    自动化要做断言,原本打算使用的testng,因为它断言后就能出结果,还能生成报告,但是在实践过程中,硬是没有成功,所以还是放弃,使用的junit,后面使用的cts框架生成的邮件,现在记录使用junit ...

  7. $GLOBALS['HTTP_RAW_POST_DATA']、$_POST和php://input深入探究三者的区别

    $_POST:通过 HTTP POST 方法传递的变量组成的数组.是自动全局变量. $GLOBALS['HTTP_RAW_POST_DATA'] :总是产生 $HTTP_RAW_POST_DATA 变 ...

  8. spring的依赖注入的最常见的两种方法

    package com.lsz.spring.action; public class User { /** * set注入 */ private String username; public vo ...

  9. C# 打印多页tif

    注意点: 1.计算image对象总页数 image.GetFrameCount(FrameDimension.Page); 2.初始化当前页,并获取指定页内容 image.SelectActiveFr ...

  10. UISearchController的使用。(iOS8+)

    这种方法早就发现了,不过一致没用,今天拿过来用,发现了一些问题. 1.这个东西和表视图结合使用很方便,首先,创建新的工程,将表视图控制器作为工程的根视图,并且添加一个导航(当然,你可以不这样做,但是你 ...