HDU5938+贪心+(2016年中国大学生程序设计竞赛(杭州))

Four Operations

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


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
1T105
5length of string20
 

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
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6032  6031  6030  6029  6028 

题意
给你一个字符串s,让你把它分成五个数,依次是abcde,使得a+b-c*d/e的值最大
思路
字符串长度应该为len
len=len1+1+1+len2;
应该使a+b大,a=s(0,len1-1)+b=s[len1]或a=s[0]与b=s(1,len1)比较两个谁大;
c=s[len1+1]
d=s[len1+2];
e=s(len1+3,len-1);
这暴力枚举len1的长度,或者枚举len2的长度
求出最大
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
    int t,T=1;
    char s[100];
    long long len,ans,maxx;
    long long a,b,c,d,e;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",s);
        len=strlen(s);
        maxx=-1000000000000000000;
        for(int i=1;i<=len-4;i++)
        {
            e=0;
            for(int j=len-i;j<=len-1;j++)
                e=e*10+(s[j]-'0');
            d=s[len-i-1]-'0';
            c=s[len-i-2]-'0';
            b=0;
            for(int j=1;j<=len-i-3;j++)
                b=b*10+(s[j]-'0');
            a=s[0]-'0';
            ans=a+b;
            b=s[len-i-3]-'0';
            a=0;
            for(int j=0;j<=len-i-4;j++)
                a=a*10+(s[j]-'0');
            if(ans<(b+a))
                ans=a+b;
           // printf("%d %d %d %d %d\n",ans,c,d,e,c*d/e);;
            ans=ans-(int)(c*d/e);
            if(maxx<ans)
                maxx=ans;
        }
        printf("Case #%d: %lld\n",T++,maxx);
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值