hdu 5938 Four Operations

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5938

 

题目描述:

 

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年中国大学生程序设计竞赛(杭州)

 

 

 

题目大意:

给你一个字符串,只由1~9组成,现在把他分成5部分,在之间按顺序加入+-*/,求出结果最大的一组解

 

题目分析:

将表达式化为a+b-c*d/e

*/的优先级要比+-的优先级要高,所以我们要保证c*d/e最小,所以即需要c*d尽可能小,e尽可能大,所以c和d我们只给他分配一位,e可以分一位,两位,或三位,因为c*d肯定在1~81之间,最小值也只需要保证c*d/e=0,所以最大三位足以,结合字符串的长度来判断

 

AC代码:

 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
char s[25];
int T;
int cas=1;
int main()
{
    cin>>T;
    while(T--)
    {
        scanf("%s",s);
        long long int l=strlen(s);
        //a+b-c*d/e
        //e=1位时
        long long int a1=s[0]-'0',a2=s[l-4]-'0',b1=0,b2=0,c=s[l-3]-'0',d=s[l-2]-'0',e=s[l-1]-'0';
        for(long long int i=1;i<l-3;i++)
            b1=b1*10+s[i]-'0';
        for(long long int i=0;i<l-4;i++)
            b2=b2*10+s[i]-'0';
        long long int l1=max(a1+b1,a2+b2)-c*d/e;
        if(l>5)//e为两位时
        {
            a1=s[0]-'0';
            a2=s[l-5]-'0';

            b1=0;
            b2=0;
            c=s[l-4]-'0';
            d=s[l-3]-'0';
            e=10*(s[l-2]-'0')+s[l-1]-'0';
            for(long long int i=1;i<l-4;i++)
                b1=b1*10+s[i]-'0';
            for(long long int i=0;i<l-5;i++)
                b2=b2*10+s[i]-'0';
            long long int l2=max(a1+b1,a2+b2)-c*d/e;
            l1=max(l1,l2);
            
            if(l>6)//e为三位时
            {
                a1=s[0]-'0';
                a2=s[l-6]-'0';
                b1=0;
                b2=0;
                c=s[l-5]-'0';
                d=s[l-4]-'0';
                e=100*(s[l-3]-'0')+10*(s[l-2]-'0')+s[l-1]-'0';
                for(long long int i=1;i<l-5;i++)
                    b1=b1*10+s[i]-'0';
                for(long long int i=0;i<l-6;i++)
                    b2=b2*10+s[i]-'0';
                l2=max(a1+b1,a2+b2)-c*d/e;
                l1=max(l1,l2);
            }
        }
        printf("Case #%d: %lld\n",cas++,l1);
    }
    return 0;
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值