F - Four Operations(考虑全面)

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

这题,首先想到除数的变换,可能有一位,也可能有两位,再就是加数,可能是减号前的一个数和前面所有的也可能是第一个数和减号之前所有的。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
char ma[34];
int main()
{
   long long int T;
   scanf("%d", &T);
   for(long long int t = 1; t<=T;t++)
   {
      scanf("%s", ma);
      long long int len = strlen(ma);
      int h1 = ma[len-1] - '0';
      int h2 = ma[len-2] - '0';
      int h3 = ma[len-3] - '0';
      int h4 = ma[len-4] - '0';
       //末尾一个字符/
       long long int v1 = h2 * h3 / h1;
       long long int ans = 0;
       for(long long int i = 0;i<len-4;i++)
       {
          ans = ans * 10 + ma[i] - '0';
       }
       long long int w1 = ans + h4 - v1;
       long long int ans2 = 0;
       for(long long int i=1;i<=len-4;i++)
       {
         ans2 = ans2 * 10 + ma[i] - '0';
       }
       long long int w2 = ans2 + ma[0] - '0' - v1;
       long long int wa;
       if(w1>w2)
       wa = w1;
       else
       wa = w2;
       //末尾两个字符/
       long long int v1b = h4 * h3 / (h2 * 10 + h1);
       long long int ans1b = 0;
       for(long long int i = 0;i<len-5;i++)
       {
          ans1b = ans1b * 10 + ma[i] - '0';
       }
       long long int w1b = ans1b + ma[len-5] - '0' - v1b;
       long long int ans2b = 0;
       for(long long int i=1;i<=len-5;i++)
       {
         ans2b = ans2b * 10 + ma[i] - '0';
       }
       long long int w2b = ans2b + ma[0] - '0' - v1b;
       long long int wab;
       if(w1b>w2b)
       wab = w1b;
       else
       wab = w2b;
       long long int wb;
       if(wab>wa)
       wb = wab;
       else
       wb = wa;
       if(len>=6)
       printf("Case #%d: %lld\n", t, wb);
       else
       printf("Case #%d: %lld\n", t, wa);
   }
   return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值