[笔试训练](三)

007

简写单词_牛客题霸_牛客网 (nowcoder.com)

题目:

 

题解:

该复合词中有空格,因此相当于以空格为分割依次输入多个字符串,字符串第一个字符为要大写并提出的字符。

#include <iostream>
using namespace std;

int main() 
{
    string s;
    while(cin>>s) //自动跳过空格
    {
        if(s[0]>='a' && s[0]<='z') cout<<(char)(s[0]-32);
        else cout<<s[0];
    }
    return 0;
}

 008

dd爱框框 (nowcoder.com)

题目:

题解:

1.暴力解法:两层for循环,先固定左端点i,依次向后找符合条件的右端点j。

2.滑动窗口(同向双指针):

1)进窗口:(区间和)sum+=arr[right]

2)判断:sum>=x

        3)更新结果:right-left+1>retlen

        4)出窗口:sum-=arr[left]

#include <iostream>
using namespace std;

const int N=1e7+9;
int arr[N];
int n,x;
int main()
{
    cin>>n>>x;
    int arr[n];
    for(int i=1;i<=n;i++)
    {
        cin>>arr[i];
    }
    int left=1,right=1,sum=0;
    int retlen=N,retleft=-1,retright=-1;
    while(right<=n)
    {
        sum+=arr[right]; //进窗口
        while(sum>=x)
        {
            if(right-left+1<retlen) //判断相同时,不会更新
            {
                retlen=right-left+1; //更新区间长度
                retleft=left;    //记录符合条件的端点
                retright=right;
            }
            sum-=arr[left++]; //出窗口
        }
        right++;
    }
    cout<<retleft<<" "<<retright<<endl;
    return 0;
}

 009

除2! (nowcoder.com)

题目:

 

题解:

 模拟+堆+贪心:创建一个只存偶数的堆,每次拿堆顶数出来,除2后,若还是偶数则放回堆中,同时数组中所有数之和sum减去减少的。

#include<iostream>
#include<queue>
using namespace std;

typedef long long LL;

LL n,k;
priority_queue<LL> heap;

int main()
{
    cin>>n>>k;
    LL sum=0,x;
    while(n--)
    {
        cin>>x;
        sum+=x;
        if(x%2==0) heap.push(x);
    }
    while(heap.size() && k--)
    {
        LL t=heap.top()/2;
        sum-=t;
        heap.pop();
        if(t%2==0)
            heap.push(t);
    }
    cout<<sum<<endl;
    return 0;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲敲er

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值