SOJ 1050. Numbers & Letters

Source: Internet
Author: User
Tags mul

Title: Give 5 integers and 4 operations (addition, subtraction, multiplication and division), select the part number as any operation, so that the value is equal to the target number or nearest to the target number. Where division can only be divisible, that is, the quotient must be an integer.

Problem-Solving ideas: Select any two numbers at a time, perform the feasible operation, obtain the result and compare with the target number. Finally come to an answer.

The code is as follows:

1#include <iostream>2 //#include <ctime>3#include <vector>4#include <climits>5 using namespacestd;6 7 Const intMAXN =5;8 intTarget;9 intapproximation;Ten intN; One  A BOOLTestConst int&Res) { -     BOOLFlag =false;; -     if(res = =target) { theapproximation =Res; -Flag =true; -}Else if(Res <target) { -approximation = approximation > res?Approximation:res; +     } -     returnFlag; + } A  at intAddConst int& A,Const int&b) { -     returnA +b; - } -  - intSubConst int& A,Const int&b) { -     returnAb; in } -  to intMulConst int& A,Const int&b) { +     returnAb; - } the  * intDivConst int& A,Const int& B,BOOL&flag) { $     intres =0;Panax Notoginseng  -     if(A <b) { the         if(A! =0&& B% A = =0) { +res = b/A; AFlag =true; the}Else { +Flag =false; -         } $}Else { $         if(b! =0&& a% b = =0) { -res = A/b; -Flag =true; the}Else { -Flag =false;Wuyi         } the     } -  Wu     returnRes; - } About  $ voidDealConstvector<int> &nums) { -     if(approximation = = target)return; -     if(Nums.size () <2)return; -  A      for(inti =0; I < nums.size (); i++) { +          for(intj = i +1; J < Nums.size (); J + +) { thevector<int>nnums; -Nnums.insert (Nnums.end (), Nums.begin (), Nums.begin () +i); $Nnums.insert (Nnums.end (), nums.begin () + i +1, Nums.begin () +j); theNnums.insert (Nnums.end (), Nums.begin () + j +1, Nums.end ()); the  the             intRes; theres =Add (Nums[i], nums[j]); -             if(Test (RES))return; in Nnums.push_back (res); the deal (nnums); the  About Nnums.pop_back (); the  theres =Sub (nums[i], nums[j]); the             if(Test (RES))return; + Nnums.push_back (res); - deal (nnums); the Bayi Nnums.pop_back (); the  theres =Sub (nums[j], nums[i]); -             if(Test (RES))return; - Nnums.push_back (res); the deal (nnums); the  the Nnums.pop_back (); the  -res =Mul (Nums[i], nums[j]); the             if(Test (RES))return; the Nnums.push_back (res); the deal (nnums);94  the Nnums.pop_back (); the  the             BOOLFlag;98res =Div (nums[i], nums[j], flag); About             if(Flag = =false)Continue; -             if(Test (RES))return;101 Nnums.push_back (res);102 deal (nnums);103 104 Nnums.pop_back (); the 106 107         }108     }109 } the 111 intMain () { theCIN >>N;113      while(n--) { thevector<int>nums; the         inttemp; the          for(inti =0; i < MAXN; i++) {117CIN >>temp;118 Nums.push_back (temp);119         } -CIN >>Target;121approximation =-Int_max;122 123         //Double start = clock ();124 deal (nums); the         //Double end = Clock ();126         //cout << (end-start)/clocks_per_sec << Endl;127  -cout << approximation <<Endl;129  the     }131     return 0; the}

SOJ 1050. Numbers & Letters

Related Article

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.