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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU Problem - 5938 Four Operations

發(fā)布時間:2024/4/18 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU Problem - 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 55 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 TT, which indicates the number of test cases.Every test contains one line with a string only contains digits ‘1’-‘9’.Limits1T1051≤T≤1055length of string205≤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

112345

Sample Output

Case #1: 1

AC

  • 因為固定順序,所以只有一種形式:A + B - C * D / E,貪心使得(A+ B)越大,(C* D / E)越小,結(jié)果的大小關(guān)鍵在于(A + B),所以C、D、E都只占一位數(shù)最好,但是有可能出現(xiàn)類似9 * 9 / 1的情況,所以比較兩種方法,一個是C、D、E分別占一位數(shù),一個是C、D都分別占一位數(shù),E占兩位
#include <iostream>#include <stdio.h>#include <map>#include <vector>#include <algorithm>#define N 100005#define ll long longusing namespace std;ll num(char c) {return c - '0';}string s;// 貪心得到和的最大值 ll get_sum(int l, int r) {if (r - l < 1) return -1e15;ll t1 = 0, t2 = 0, t3 = 0, t4 = 0;for (int i = l; i < r; ++i) {t1 = t1 * 10 + s[i] - '0';}t2 = s[r] - '0';for (int i = l + 1; i <= r; ++i) {t3 = t3 * 10 + s[i] - '0';}t4 = s[l] - '0';return max(t1 + t2, t3 + t4);}int main() {#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endifint t;scanf("%d", &t);int Case = 1;while (t--) {ll ans1, ans2;cin >> s;int len = s.size(); ans1 = get_sum(0, len - 4) - num(s[len - 3]) * num(s[len - 2]) / num(s[len - 1]);ans2 = get_sum(0, len - 5) - num(s[len - 4]) * num(s[len - 3]) / (num(s[len - 2]) * 10 + num(s[len - 1]));printf("Case #%d: %lld\n", Case++, max(ans1, ans2));}return 0;}

總結(jié)

以上是生活随笔為你收集整理的HDU Problem - 5938 Four Operations的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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