牛客编程巅峰赛S2第5场

A 牛牛算数

链接:https://ac.nowcoder.com/acm/contest/9556/A
来源:牛客网

题目描述
给你一个含有n个元素的数组arr[i],请你告诉牛牛这个数组的中位数大还是平均数大,如果中位数更大输出1,如果平均数更大输出-1,如果中位数和平均数相等输出0

示例1
输入
复制
[1,3,4]
返回值
复制
1
说明
中位数3,平均数约等于2.67,所以输出1
示例2
输入
复制
[7,4,8,11]
返回值
复制
0
说明
中位数7.5,平均数7.5,所以输出0
示例3
输入
复制
[6,6,6,6,5,8]
返回值
复制
-1
说明
中位数6,平均数约等于6.17,所以输出-1
备注:
对于30%30%的数据: 1\leq n\leq 1e3,1\leq arr[i]\leq 1e91≤n≤1e3,1≤arr[i]≤1e9
对于100%100%的数据: 1\leq n\leq 1e6,1\leq arr[i]\leq 1e91≤n≤1e6,1≤arr[i]≤1e9

大数,python走起

#
# 
# @param arr int整型一维数组 
# @return int整型
#
class Solution:
    def Answerofjudge(self , arr ):
        # write code here
        arr.sort()
        n = len(arr)
        avg = sum(arr) / n
        mid = 0
        if n&1:
            mid = arr[n // 2 - 1]
        else:
            mid = (arr[n// 2 - 1] + arr[n // 2]) / 2
        
        if avg == mid:
            return 0
        elif mid > avg:
            return 1
        else:
            return - 1

B 怕npy的牛牛

链接:https://ac.nowcoder.com/acm/contest/9556/B
来源:牛客网

题目描述
牛牛非常怕他的女朋友,怕到了走火入魔的程度,以至于每当他看到一个字符串同时含有n,p,y三个字母他都害怕的不行。现在有一个长度为m的只包含小写字母‘a’-‘z’的字符串x,牛牛想知道能令他不害怕的最长子串的长度是多少。(对于字符串”abc”来说,”c”,”ab”都是原串的子串,但”ac”不是原串子串)

示例1
输入
复制
“abcdefghijklmn”
返回值
复制
14
说明
因为所有子串都不同时含有n,p,y,所以最长子串的长度即为字符串x的长度14。

示例2
输入
复制
“ynp”
返回值
复制
2
说明
长度为2的字串”yn”,”np”都符合题意,不存在长度>=3的符合条件的子串。

示例3
输入
复制
“ypknnbpiyc”
返回值
复制
7
说明
“pknnbpi”为其符合条件的最长子串,长度为7。

备注:
对于40%40%的数据1\leq m\leq 1001≤m≤100

对于100%100%的数据1\leq m\leq 1, 000,0001≤m≤1000000

函数共有一个参数,即题目描述中的字符串x,保证字符串中字母均为小写字母
注意,所给字符串不含引号

双指针,维护一段同时不包含n p y的字符串即可


class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 返回符合题意的最长的子串长度
     * @param x string字符串 
     * @return int整型
     */
    unordered_map<char, int> mp;
    bool check(){
        if(mp['n'] >= 1 && mp['p'] >= 1 && mp['y'] >= 1)
            return true;
        return false;
    }
    int Maximumlength(string x) {
        // write code here
        int n = x.size();
        int res = 0;
        for(int i = 0, j = 0; j < n; j++){
            mp[x[j]] ++;
            while(i < j && check()){
                
                mp[x[i]]--;
                i ++;
            }
            res = max(res, j - i + 1);
        }
        return res;
    }
};

牛牛与后缀表达式

在这里插入图片描述
链接:https://ac.nowcoder.com/acm/contest/9556/C
来源:牛客网

示例1
输入
复制
“1#1#+”
返回值
复制
2
说明
1#1#+这个后缀表达式表示的式子是1+1,结果为2
示例2
输入
复制
“12#3#+15#*”
返回值
复制
225
说明
12#3#+15#*这个后缀表达式表示的式子是(12+3)*15,结果为225
备注:
1\leq表达式中操作数\leq10^91≤表达式中操作数≤10
9

1\leq表达式长度\leq10^61≤表达式长度≤10
6

很迷的一道题
下述代码可以过,但改成牛客网就过不了了,不知道是不是因为case的原因。赛后改成if就过了。

#include<bits/stdc++.h>
using namespace std;
#define N 1000005
string s;
long long stackk[N];
int main()
{

 
 int top=0, i=0, x;
 cin>>s;
  s += '"';
 memset(stackk, 0, sizeof(stackk));
 while(s[i]!='"'){
     switch(s[i]){
        case'+':stackk[--top]+=stackk[top+1];break;
        case'-':stackk[--top]-=stackk[top+1];break;
        case'*':stackk[--top]*=stackk[top+1];break;
        
        default:x=0;while(s[i]!='#')x=x*10+s[i++]-'0';
        stackk[++top]=x;break;
     }
     i++;
 }

 cout<<stackk[top]<<endl;
  return 0;
}
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 给定一个后缀表达式,返回它的结果
     * @param str string字符串 
     * @return long长整型
     */
    typedef long long ll;
    stack<ll> s;
    long long solve(string str) {
        // write code here
        int top=0, i=0;
        ll x;
        str += '"';
    
        while(str[i] != '"'){
             if(str[i] == '+'){
                 ll a = s.top();
                 s.pop();
                 ll b = s.top();
                 s.pop();
                 s.push(a + b);
             }
             else if(str[i] == '-'){
                 ll a = s.top();
                 s.pop();
                 ll b = s.top();
                 s.pop();
                 s.push(b - a);
             }
             else if(str[i] == '*'){
                 ll a = s.top();
                 s.pop();
                 ll b = s.top();
                 s.pop();
                 s.push(a * b);
             }
            else{
                x=0;
                while(str[i] != '#') 
                    x = x * 10 + str[i++]-'0';
                s.push(x);
            }
            i++;
        }
        ll w = s.top();
        return w;
    }
};
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值