HDU 5938 Four Operations (水题乱搞)

Four Operations

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 413 Accepted Submission(s): 149

Problem Description
Little 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 ‘/’ in order, then calculate the result(/ used as integer division).

Now please help him to get the largest result.

Input
First line contains an integer T, which indicates the number of test cases.

Every test contains one line with a string only contains digits ‘1’-‘9’.

Limits
1≤T≤105
5≤length of string≤20

Output
For every test case, you should output ‘Case #x: y’, where x indicates the case number and counts from 1 and y is the result.

Sample Input
1
12345

Sample Output
Case #1: 1

Source
2016年中国大学生程序设计竞赛(杭州)

Recommend
liuyiding | We have carefully selected several similar problems for you: 5960 5959 5958 5957 5956

要把+-*/按顺序填入给定字符串,使式子结果最大。这个思考起来还是比较容易的,符号填入的位置都是固定的几种组合,代码里已经很清楚了。。

#include "cstring"
#include "cstdio"
#include "iostream"
#include "string.h"
#include "cmath"
#include "algorithm"
using namespace std;
char s[50];
long long getnum(int start,int end)
{
    long long sum=0;
    long long temp=1;
    for(int i=end;i>=start;i--)
    {
        sum+=temp*(s[i]-'0');
        temp*=10;
    }
    return sum;
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int k=1;k<=t;k++)
    {
        scanf("%s",s);
        int len=strlen(s);
        if(len==5)
        {
            printf("Case #%d: %d\n",k,getnum(0,0)+getnum(1,1)-getnum(2,2)*getnum(3,3)/getnum(4,4));
            continue;
        }
        long long back1=getnum(len-3,len-3)*getnum(len-2,len-2)/getnum(len-1,len-1);
        long long back2=getnum(len-4,len-4)*getnum(len-3,len-3)/getnum(len-2,len-1);
        long long front11=getnum(0,0)+getnum(1,len-4);
        long long front12=getnum(0,len-5)+getnum(len-4,len-4);
        long long front21=getnum(0,0)+getnum(1,len-5);
        long long front22=getnum(0,len-6)+getnum(len-5,len-5);
        long long ans=-1;
        ans=max(ans,front11-back1);
        ans=max(ans,front12-back1);
        ans=max(ans,front21-back2);
        ans=max(ans,front22-back2);
        printf("Case #%d: %lld\n",k,ans);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值