[Sicily online] 1050. Numbers & letters

Source: Internet
Author: User
Constraints

Time Limit: 3 secs, memory limit: 32 MB

Description

In the early 80's, a popular TV show on Dutch television was 'cijfers en letters '(numbers and letters ). this game consisted of two game elements, in which the main goal was to outclass your opponent. letters is a game in which you are given a number of letters
With which you shocould form the longest Dutch word possible. Since Dutch is a very hard language to learn we will postpone implementation of this game element until after the contest.
For the second game element 'numbers '5 different numbers are chosen, together with a target number. The aim is to use some arithmetic on (Some) The five numbers to form the target number. Each number can be used only once. It might not
Be possible to form the target number given the input numbers, in that case the largest number smaller than the target number that can be calculated shocould be given. the only mathematical operations allowed are: + ,-,*,/. all intermediate results shocould
Be integers, so division is not always allowed (e.g. (2*2)/4 is OK, but 2 * (2/4) is not ).
Examples:
-If the 5 numbers are 1, 2, 3, 7 and 100 and the target number is 573, the target number can be reached as follows: (100-1) * 2)-7) * 3. -If the 5 numbers are 3, 26, 78, 12 and 17, and the target number is 30, the target number can be reached as follows: (78*3) -(12*17 ).
-If the 5 numbers are 67, 69, 58, 22, 2, and the target number is 929, the target number cannot be reached, but the largest number smaller than the target number that can be reached is 923 = (22-(67-58) * (69 + 2 ).
Your assignment is to write a program that calculates the Best Approximation from below of the target number using Arithmetic on the 5 given numbers. note that if it is not possible to reach the exact number, you shoshould give the largest reachable number below
The target number.

Input

The first line contains the number of runs, N. the next n lines consist of six numbers separated by a space. the first 5 numbers Mi, 1 ≤mi ≤ 100, are the numbers you can use to calculate the target number. the sixth number is the target number t, 0 ≤ T ≤ 1000.

Output

The output consists of N rows, each containing the best approximation of the target number using the 5 given numbers.

Sample Input

31 2 3 7 100 5733 26 78 12 17 3067 69 58 22 2 929

Sample output

57330 923

Question Analysis:

The changed 24-point problem may not be used in all five cases. As long as the target node is found, the overall performance is stopped. DFS is better than BFs. So this topic is to use DFS to traverse all situations.

1. At the beginning, I used stack to implement DFS.

2. Later I used function Recursion TO IMPLEMENT DFS and AC.

Later, I made a comparison, and the function recursion of the system is nearly five times faster than that of the stack.

/**/# Include <iostream> # include <iomanip> # include <stdio. h> # include <cmath> # include <iomanip> # include <list> # include <map> # include <vector> # include <string> # include <algorithm> # include <sstream> # include <stack> # include <queue> # include <string. h ># include <set> using namespace STD; int Max, goal; bool DFS (vector <long> & to) {for (INT I = 0; I <. size (); I ++) {If (to [I] = goal) {return true ;} else if (to [I] <goal & to [I]> m Ax) {max = to [I] ;}} for (INT I = 0; I <. size (); I ++) {for (Int J = I + 1; j <. size (); j ++) {vector <long> basetmp; For (int K = 0; k <. size (); k ++) {If (K! = I & K! = J) basetmp. push_back (to [k]);} vector <long> plustmp (basetmp); plustmp. push_back (to [I] + to [J]); If (DFS (plustmp) return true; vector <long> minustmp1 (basetmp ); minustmp1.push _ back (to [I]-To [J]); If (DFS (minustmp1) return true; vector <long> minustmp2 (basetmp ); minustmp2.push _ back (to [J]-To [I]); If (DFS (minustmp2) return true; vector <long> multiplytmp (basetmp); multiplytmp. push_back (to [I] * to [J]); If (d FS (multiplytmp) return true; If (to [I] = 0 | to [J] = 0) continue; If (max (to [I], to [J]) % min (to [I], to [J]) = 0) {vector <long> dividetmp (basetmp); dividetmp. push_back (max (to [I], to [J])/min (to [I], to [J]); If (DFS (dividetmp )) return true ;}}return false ;}int main () {int N; // CIN >>n; scanf ("% d", & N ); for (INT xx = 0; XX <n; XX ++) {vector <long> num (5); Goal; For (INT I = 0; I <5; I ++) {// CIN> num [I]; scanf ("% d", & num [I]) ;}// CIN> Goa L; scanf ("% d", & goal); max =-9999; clock_t start = clock (); If (DFS (Num )) printf ("% d \ n", goal); else printf ("% d \ n", max); clock_t end = clock (); double total_time = (double) (end-Start)/clocks_per_sec; // printf ("% F seconds/N", total_time );}} // use the stack to implement deep Priority Search // int main () // {// int N; // CIN> N; // scanf ("% d", & N); // For (INT xx = 0; XX <n; XX ++) // {// int num [5]; // int goal; // For (INT I = 0; I <5; I ++) /// {// CIN> num [I]; // scanf ("% d", n Um + I); //} // CIN> goal; // scanf ("% d", & goal); // int max =-9999; // clock_t start = clock (); // bool flag = false; // stack <vector <int> st; // vector <int> Init (Num, num + 5); // st. push (init); // while (! St. empty () // {// vector <int> to = ST. top (); // st. pop (); // For (INT I = 0; I <. size (); I ++) // {// If (to [I] = goal) // {// flag = true; // break; /// else if (to [I] <goal & to [I]> MAX) // {// max = to [I]; //} // For (INT I = 0; I <. size (); I ++) // {// For (Int J = I + 1; j <. size (); j ++) // {// vector <int> basetmp; // For (int K = 0; k <. size (); k ++) // {// If (K! = I & K! = J) // basetmp. push_back (to [k]); //} // vector <int> plustmp (basetmp); // plustmp. push_back (to [I] + to [J]); // st. push (plustmp); // vector <int> minustmp1 (basetmp); // minustmp1.push _ back (to [I]-To [J]); // st. push (minustmp1); // vector <int> minustmp2 (basetmp); // minustmp2.push _ back (to [J]-To [I]); // st. push (minustmp2); // vector <int> multiplytmp (basetmp); // multiplytmp. push_back (to [I] * to [J]); // st. push (multiplytmp); // If (to [I] = 0 | to [J] = 0) // continue; // If (max (to [I], to [J]) % min (to [I], to [J]) = 0) /// {// vector <int> dividetmp (basetmp); // dividetmp. push_back (max (to [I], to [J])/min (to [I], to [J]); // st. push (dividetmp); //} // If (FLAG) // printf ("% d \ n", goal ); // else printf ("% d \ n", max); // clock_t end = clock (); // double total_time = (double) (end-Start)/clocks_per_sec; // printf ("% F seconds/N", total_time );//}//}

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.