日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 5938 Four Operations 【字符串处理,枚举,把数字字符串变为数值】

發(fā)布時間:2024/4/14 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 5938 Four Operations 【字符串处理,枚举,把数字字符串变为数值】 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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


【題意】:給一個字符串,按+, -, *, /的順序插入將字符串分成a+b-c*d/e,要求結(jié)果最大。
【分析】:枚舉負(fù)號的位置,因?yàn)橐拐麄€值最大,C*D應(yīng)該最小,所以C和D都只取一位。
A+B的值最大需要使A或B的位數(shù)盡可能大,即A一位,B為到負(fù)號前的所有位或者B為符號前一位,
A為開頭到負(fù)號前一位位置,兩種情況取最大值。由于C和D都確定了E也隨之確定了。
所以總的來說就是枚舉一下負(fù)號的位置,然后判斷一下A+B的最大值就可以了。?
【代碼】:
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <string>const int maxn=25; const int inf=0x3f3f3f3f; typedef long long ll; using namespace std;char a[maxn]; int num[maxn];ll f(int s,int e) {ll res=0;for(int i=s; i<=e; i++){res = res*10 + num[i] ;}return res; }// int main() {int t;scanf("%d",&t);for(int cas=1;cas<=t;cas++){scanf("%s",a);int len=strlen(a);ll ans = -inf;for(int i=0;i<len;i++){num[i+1]=a[i]-'0';}for(int i=2;i<=len-3;i++){ll add,c,d,e;add=max((f(1,i-1)+f(i,i)) , (f(1,1)+f(2,i)));c=num[i+1];d=num[i+2];e=f(i+3,len);ans=max(ans,add-c*d/e);}printf("Case #%d: %lld\n",cas,ans);}return 0; } View Code

?

轉(zhuǎn)載于:https://www.cnblogs.com/Roni-i/p/7505345.html

總結(jié)

以上是生活随笔為你收集整理的HDU 5938 Four Operations 【字符串处理,枚举,把数字字符串变为数值】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。