Four Operations
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): Accepted submission (s): 12
Problem Descriptionlittle Ruins is a studious boy, recently he learned the four operations!
Now he want to use four operations to generate a number, he takes a string which only contains digits
' 1 '-
' 9 ', and split it into 5 intervals and add the four operations
' + ',
'-',
' * ' and
'/' on order, then C Alculate the result (/used as Integer division).
Now you are him to get the largest result.
Inputfirst line contains an integer T, which indicates the number of test cases.
Every test contains one line with a string of only contains digits
' 1 '-
' 9 '.
Limits
1≤T≤5
5≤length of string≤
Outputfor every test case, should output
' case #x: Y ', where
xIndicates the case number and counts from
1and
yis the result.
Sample Input112345
Sample outputcase #1:1
SOURCE2016 Chinese College Student Program Design Competition (Hangzhou)
recommendliuyiding | We have carefully selected several similar problems for you:5943 5942 5941 5940 5939
Statistic | Submit | Discuss | Note
Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=5938
Main topic:
A string of strings of length 5~20 (1~9 only) is required to sequentially insert a +-*/each into a number, making the result of the final operation maximum.
Topic Ideas:
Greedy
Analysis *, because the front is-number so want to multiply the number of small, so multiply and-/between only 1 bits.
The analysis/, in addition to only possible except 1 digits or 2 digits (because multiply only 1 bits, 3 bit suboptimal, 2 bit may be 111991, originally I thought as long as/one).
Analysis +, you can know that the greater the number of digits plus the greater the result, so + can only be 1-digit and multi-digit addition.
So we can classify the discussion, 1-bit or 2-bit, multiply close to/before,-close to *, before the number is either in the first break, or the last one.
The answer can be maximized.
1 //2 //by Coolxxx3 //#include <bits/stdc++.h>4#include <iostream>5#include <algorithm>6#include <string>7#include <iomanip>8#include <map>9#include <stack>Ten#include <queue> One#include <Set> A#include <bitset> -#include <memory.h> -#include <time.h> the#include <stdio.h> -#include <stdlib.h> -#include <string.h> - //#include <stdbool.h> +#include <math.h> - #pragmaComment (linker, "/stack:1024000000,1024000000") + #defineMin (a) < (b) ( A):(B)) A #defineMax (a) (a) > (b)? ( A):(B)) at #defineABS (a) ((a) >0? ( A):(-(a))) - #defineLowbit (a) (a& (a)) - #defineSqr (a) ((a) * (a)) - #defineSwap (a) (a) ^= (b), (b) ^= (a), (a) ^= (b)) - #defineMem (A, B) memset (A,b,sizeof (a)) - #defineEPS (1E-8) in #defineJ 10000 - #defineMoD 1000000007 to #defineMAX 0x7f7f7f7f + #definePI 3.14159265358979323 - #defineN 24 the using namespacestd; *typedefLong LongLL; $typedef unsignedLong LongULL;Panax Notoginseng DoubleAnss; - LL Aans; the intCas,cass; + intN,m,lll,ans; A LL sum,tot,s1,s2; the CharS[n]; + intMain () - { $ #ifndef Online_judgew $ //freopen ("1.txt", "R", stdin); - //freopen ("2.txt", "w", stdout); - #endif the inti,j,k; - intx, y, zWuyi //init (); the //for (scanf ("%d", &cass); cass;cass--) - for(SCANF ("%d", &cas), cass=1; cass<=cas;cass++) Wu //while (~scanf ("%s", s)) - //while (~scanf ("%d%d", &n,&m)) About { $tot=0; sum=0; s1=0; s2=0; -printf"Case #%d:", Cass); -scanf"%s", s); -n=strlen (s); Atot=-(s[n-3]-'0') * (s[n-2]-'0')/(s[n-1]-'0'); +s1=s[0]-'0'; s2=0; the for(i=1; i<n-3; i++) -s2=s2*Ten+s[i]-'0'; $sum=tot+s1+S2; thes1=s[n-4]-'0'; s2=0; the for(i=0; i<n-4; i++) thes2=s2*Ten+s[i]-'0'; thetot+=s1+S2; -sum=Max (sum,tot); in if(n>5) the { thetot=-(s[n-4]-'0') * (s[n-3]-'0')/((s[n-2]-'0')*Ten+s[n-1]-'0'); Abouts1=s[0]-'0'; s2=0; the for(i=1; i<n-4; i++) thes2=s2*Ten+s[i]-'0'; thetot+=s1+S2; +sum=Max (sum,tot); -tot=-(s[n-4]-'0') * (s[n-3]-'0')/((s[n-2]-'0')*Ten+s[n-1]-'0'); thes1=s[n-5]-'0'; s2=0;Bayi for(i=0; i<n-5; i++) thes2=s2*Ten+s[i]-'0'; thetot+=s1+S2; -sum=Max (sum,tot); - } theprintf"%lld\n", sum); the } the return 0; the } - /* the // the the //94 */
View Code
HDU 5938 four Operations "greedy" (2016 Chinese university Student Program Design Competition (Hangzhou))