zoukankan      html  css  js  c++  java
  • poj 1348 Computing (四个数的加减乘除四则运算)

    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)获取下一个字典序
     
    代码:
      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[5];  //以 分子/分母 形式保存一个数
     13 
     14 
     15 void getNum(int *a)  //将a数组转换成 分子/分母 形式
     16 {
     17     int i;
     18     for(i=0;i<4;i++)
     19     {
     20         num[i].son = a[i];
     21         num[i].mon = 1;
     22     }
     23 }
     24 
     25 Nod operate(Nod a,Nod b,int ch)  //进行四则运算
     26 {
     27     Nod temp;
     28     if(ch==0)  // '+'
     29     {
     30         temp.mon = a.mon * b.mon;
     31         temp.son = a.son * b.mon + b.son * a.mon;
     32     }
     33     else if(ch==1)  // '-'
     34     {
     35         temp.mon = a.mon * b.mon;
     36         temp.son = a.son * b.mon - b.son * a.mon;
     37     }
     38     else if(ch==2)  //  '*'
     39     {
     40         temp.mon = a.mon * b.mon;
     41         temp.son = a.son * b.son;
     42     }
     43     else if(ch==3)  //  '/'
     44     {
     45         temp.mon = a.mon * b.son;
     46         temp.son = b.mon * a.son;
     47     }
     48     return temp;
     49 }
     50 
     51 int computing(int *a,int e)
     52 {
     53     getNum(a);  //获得  分子/分母 的表示方式
     54     Nod temp1,temp2,temp3;
     55     int i,j,k;
     56 
     57     // ((a#b)#c)#d   '#'号代表运算符号
     58     for(i=0;i<4;i++)
     59     {
     60         temp1 = operate(num[0],num[1],i);
     61         if(temp1.mon == 0)  continue;   //分母为0情况
     62         for(j=0;j<4;j++)
     63         {
     64             temp2 = operate(temp1,num[2],j);
     65             if(temp2.mon == 0) continue;
     66             for(k=0;k<4;k++)
     67             {
     68                  temp3 = operate(temp2,num[3],k);
     69                  if(temp3.mon == 0) continue;
     70                  if(temp3.son%temp3.mon==0&&temp3.son/temp3.mon==e) return 1;
     71             }
     72         }
     73     }
     74 
     75     //(a#(b#(c#d)))
     76     for(i=0;i<4;i++)
     77     {
     78         temp1 = operate(num[2],num[3],i);
     79         if(temp1.mon == 0)  continue;
     80         for(j=0;j<4;j++)
     81         {
     82             temp2 = operate(num[1],temp1,j);
     83             if(temp2.mon == 0) continue;
     84             for(k=0;k<4;k++)
     85             {
     86                  temp3 = operate(num[0],temp2,k);
     87                  if(temp3.mon == 0) continue;
     88                  if(temp3.son%temp3.mon==0&&temp3.son/temp3.mon==e) return 1;
     89             }
     90         }
     91     }
     92     //(a#b)#(c#d)
     93     for(i=0;i<4;i++)
     94     {
     95         temp1 = operate(num[0],num[1],i);
     96         if(temp1.mon == 0)  continue;
     97         for(j=0;j<4;j++)
     98         {
     99             temp2 = operate(num[2],num[3],j);
    100             if(temp2.mon == 0) continue;
    101             for(k=0;k<4;k++)
    102             {
    103                  temp3 = operate(temp1,temp2,k);
    104                  if(temp3.mon == 0) continue;
    105                  if(temp3.son%temp3.mon==0&&temp3.son/temp3.mon==e) return 1;
    106             }
    107         }
    108     }
    109     return 0;
    110 }
    111 
    112 int main()
    113 {
    114     int a[5],e;
    115     while(~scanf("%d",&a[0])&&a[0]!=-1)
    116     {
    117         scanf("%d%d%d%d",&a[1],&a[2],&a[3],&e);
    118         int i,j;
    119         for(j=0;j<4;j++) printf("%d ",a[j]);
    120         for(i=0;i<24;i++)
    121         {
    122             if(computing(a,e) == 1) break;
    123             next_permutation(a,a+4);   //获取下一个字典序
    124         }
    125         printf("%d ",e);
    126         if(i<24) puts("OK!");
    127         else puts("NO!");
    128     }
    129     return 0;
    130 }
     
  • 相关阅读:
    序列——堆排序-大根堆(堆大顶)
    3 分钟的高速体验 Apache Spark SQL
    UVa 1401 Remember the Word
    leetcode
    oracle看到用户的所有表名、表睐、字段名称、现场的目光、是空的、字段类型
    cocos2d-x物业现场
    Unable to start MySQL service. Another MySQL daemon is already running with the same UNIX socket
    ThreadPoolExecutor详解
    Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
    A First Exploration Of SolrCloud
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3222686.html
Copyright © 2011-2022 走看看