2016ccpc杭州赛 hdu 5938 F.Four Operations

本文介绍了一道编程竞赛题目,任务是通过四则运算使给定数字串生成的最大值。文章提供了完整的代码实现和解题思路,指出关键在于合理分割数字串并选择运算符。


Four Operations

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


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:   5943  5942  5941  5940  5939 
 

Statistic |  Submit |  Discuss |  Note
题意:给你一个字符串,把它分成四块,按顺序把符号+ - * /放上去,问能得到的最大数是多少。

思路:又是一道水题。首先我们看*和/,因为前面有-,所以后面的数要尽可能的小。那么相乘要最小肯定是一位数乘一位数。除的话有两种情况,可能除一位数最小可能除两位数最小,因为一位数乘一位数不可能得到三位数,如果除一个三位数会浪费了一位,然后就是加的部分了,加得到的数最大肯定是一位数加多位数或者多位数加一位数,那么现在答案很明显就只有4种情况,4种求出来取最大就是答案了,下面给代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<utility>
#include<map>
#define maxn 25
typedef long long LL;
using namespace std;
const int mod=1e9+7;;
char s[maxn];
int a[maxn];
LL getnum(int l,int r){
    LL ans=0;
    for(int i=l;i<=r;i++){
        ans=(ans*10)+a[i];
    }
    return ans;
}
int main(){
    int t;
    scanf("%d",&t);
    for(int tcase=1;tcase<=t;tcase++){
        scanf("%s",s);
        int len=strlen(s);
        for(int i=0;i<len;i++){
            a[i]=s[i]-'0';
        }
        LL ans=a[0]+getnum(1,len-4)-a[len-3]*a[len-2]/a[len-1];
        ans=max(ans,getnum(0,len-5)+a[len-4]-a[len-3]*a[len-2]/a[len-1]);
        if(len>5){
            int div=getnum(len-2,len-1);
            ans=max(ans,a[0]+getnum(1,len-5)-a[len-4]*a[len-3]/div);
            ans=max(ans,getnum(0,len-6)+a[len-5]-a[len-4]*a[len-3]/div);
        }
        printf("Case #%d: %lld\n",tcase,ans);
    }
}


CCPC女生程序设计竞即中国大学生程序设计竞女生专场,不同年份的CPCP女生有不同的题目及解题思路。 2021年的CCPC女生有公交路线题,解题代码通过输入公交路线相关信息,根据不同条件判断输出结果,当`x < y`和`x >= y`时分别进行不同的判断逻辑,最终输出“Wrong”“Right”或“Unsure”[^3]。 ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, x, y, arr[15]={0}; cin >> n >> x >> y; for (int i = 1; i <= n; i++) { cin >> arr[i]; } int m, xx, brr[15]={0}; cin >> m; if(x<y){ for(int i=1;i<=m;i++){ cin>>brr[i]; if(brr[i]!=arr[i+x]){ cout<<"Wrong"<<endl; return 0; } } if(x-1<1||x-m<1){ cout<<"Right"<<endl; return 0; } for(int i=1;i<=m;i++){ if(arr[x-i]!=brr[i]){ cout<<"Right"<<endl; return 0; } } cout<<"Unsure"<<endl; return 0; }else{ for(int i=1;i<=m;i++){ cin>>brr[i]; if(brr[i]!=arr[x-i]){ cout<<"Wrong"<<endl; return 0; } } if(x==n||x+m>n){ cout<<"Right"<<endl; return 0; } for(int i=1;i<=m;i++){ if(arr[x+i]!=brr[i]){ cout<<"Wrong"<<endl; return 0; } } cout<<"Unsure"<<endl; } return 0; } ``` 2023年的CCPC女生,有一道题的解题思路是对样例进行模拟,从最小数开始排,在数组中碰到前后相等的数就从后往前排,当数组中所给的数比它前边的数小,就判断为“ -1”。也可当成一个思维题,从最小的递增子序列长度从一开始排列,碰到一样长度的就从后往前排列,当递增序列断档不连续时该序列不合法[^2][^4]。 ```cpp #include "vector" using namespace std; const int N = 1e7; vector<int > v[N]; int a[N], b[N]; int main(){ int n; ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); int max = 0, flag = 0; cin>>n; for(int i = 1; i <= n; i++){ cin>>a[i]; if(a[i] > max) { if(a[i] - max > 1) flag = 1; max = a[i]; } v[a[i]].push_back(i); } if(flag) cout<<"-1"<<endl; else { int p = 1; for(int i = 1; i <= max; i++) for(int j = v[i].size() - 1; j >= 0; j --) { int t = v[i][j]; b[t] = p ++; } for(int i = 1; i <= n; i++) cout<<b[i]<<' '; cout<<endl; } } ``` 2024年的CCPC女生,有题目解题思路是观察到最优解是尽可能高效率地使用`C`与`P`,每个`CCPC`固定一个`P`,另外的`C`看公共前后缀,通过模拟实现,代码通过统计字符串中`C`和`P`的数量,输出`P`的数量和`(C的数量 - 1) / 2`中的较小值[^1]。 ```cpp string s; cin >> s; map<char,int> mp; for(int i = 0;i<sz(s);i++)mp[s[i]]++; cout << min(mp['P'] , (mp['C'] - 1) / 2) <<endl; return 0; ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值