Problem solving report--2018 level 2016 first week of second semester homework

Source: Internet
Author: User

Problem Solving report -level 2018 2016 the first week of the second semester homework

D calculation

Title Description

Describe

given 4 less than 10 positive integers, you can use subtraction 4 operations and parentheses to concatenate the 4 numbers together to get an expression. The question now is whether there is a way to make the resulting expression equal to 24.

Here the subtraction and parentheses are given the same precedence as our usual definition (except for the legal meaning of the real division).

For example, for 5,5,5,1, we know that 5 * (5–1/5) = 24, so we can get 24. Another example, for 1,1,4,2, we can not get 24.

Input

The input data includes multiple lines, each giving a set of test data, including 4 less than 10 positive integers. The last set of test data includes 4 0, which indicates the end of the input, and this set of data is not processed.

Output

for each set of test data, output a row, if you can get 24, output "YES", otherwise, output "NO".

Sample input

5 5 5 1

1 1 4 2

0 0 0 0

Sample output

YES

NO

Code

#include<cstdio>#include<cmath>#include<cstring>#include <iostream>#include<cstdlib>

using namespace std;

Double A[5];bool flag=false;

void Dfs(double b[],int k) {

if (flag) return ;

if (k==1) {

if (fabs(b[1]-24) <1e-4) flag=true;// correct solution

}

Else for (int i=1;i<=k;i++)

for (int j=1;j<=k;j++)

if (I!=J) {

Double Bb[5];int js=0;

//bb used to store new state

// put the number of unselected numbers into BB First

for (int p=1;p<=k;p++)

if (P!=I&&P!=J) bb[++js]=b[p];

Double Op=1e10; // used to store calculation results

for (int cf=1;cf<=4;cf++)

Switch (CF) {

case 1:op=b[i]+b[j];

Bb[++js]=op; // put the calculation results in BB

DFS (BB,JS);

js--;

break;

case 2:op=b[i]-b[j];

Bb[++js]=op;

DFS (BB,JS);

js--;

break;

case 3:op=b[i]*b[j];

Bb[++js]=op;

DFS (BB,JS);

js--;

break;

Case 4:if(b[j]!=0) {op=b[i]/b[j];

Bb[++js]=op;

DFS (BB,JS);

js--;}

break;

}

}

}

int main() {

while (1) {

Flag=false;

for (int i=1;i<=4;i++)

cin>>a[i];

if (a[1]==0) break;// whether to finish reading

DFS (a,4);

if (flag) cout<< "YES" <<endl;

else cout<< "NO" <<endl;

}

return 0;

}

Analysis

The state of the problem is the number of K ;

When there is only one number, that is the problem; all exits are a number;

possible paths: Take the four-variable three as an example +,* There are 12 kinds,/,- There are 24 kinds, There will be a total of 36;

but it will be feasible to judge: Division is a divisor of 0 when it is not possible;

however, in which to maintain the state that would use a array and k;

but in order to save time, you can go out if you have the correct solution (if le-4).

do not use BB to store the number of not selected, and then use switch to enumerate the possible four kinds of operations;

Note: Use double to store because there will be points on the topic;

Algorithm: brute-lifting algorithm;

Problem solving report--2018 level 2016 first week of second semester homework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.