笔试强训Day17 字符串 前缀和

本文分享了三道编程题目的解题思路和C++代码实现,涉及数字转换、矩阵计算(十字爆破)和前缀和查找(比那名居的桃子),展示了基础算法在实际问题中的应用。
摘要由CSDN通过智能技术生成

BC45 小乐乐改数字

题目链接:小乐乐改数字_牛客题霸_牛客网 (nowcoder.com)

思路:

水题一道 注意前导0.

AC code:

#include <iostream>
#include<string>
using namespace std;
string a,b;
int main() 
{
    cin >> a;
    for(auto& t : a)
    {
        if(t & 1) b += "1";
        else b += '0';
    }
    bool f = 0;
    for(int i = 0; i < b.size(); i ++)
    {
        if(b[i] != '0') f = 1;
        if(f) cout << b[i];
    }
    if(!f) cout << 0;

    return 0;
}

十字爆破

题目链接:I-十字爆破_牛客小白月赛25 (nowcoder.com)

思路:

预处理每行每列 相加即可。

AC code:

#include <iostream>
using namespace std;
long long typedef LL;
const int N = 1e6 + 10;
LL a[N], sl[N], sr[N]; 
LL n,m;
int main()
{
	cin >> n >> m;
	for (LL i = 0; i < n * m; i++) cin >> a[i];
	for (LL i = 0; i < n * m; i++)
	{
		sl[i / m] += a[i]; //行
		sr[i % m] += a[i]; //列
	}
	for (LL i = 0; i < n * m; i++)
	{
		cout << sl[i / m] + sr[i % m] - a[i] << ' ';
		if (i % m == m - 1) cout << endl;
	}
	return 0;
}

比那名居的桃子

题目链接:D-比那名居的桃子_牛客小白月赛37 (nowcoder.com)

思路:前缀和 快乐找最大的同时羞耻找最小即可。

AC code:

#include<iostream>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int a[N],b[N];
LL s1[N],s2[N];
int n,m;
int main()
{
    cin >> n >> m;
    
    for(int i = 1; i <= n; i ++)
    {
        cin >> a[i];
        s1[i] = s1[i - 1] + a[i];
    }
    
    for(int i = 1; i <= n; i ++)
    {
        cin >> b[i];
        s2[i] = s2[i - 1] + b[i];
    }
    int ans = 1;
    pair<LL,LL> tmp = {-3e9,3e9};
    for(int pos1 = 1, pos2 = pos1 + m - 1; pos1 + m - 1 <= n; pos1 ++)
    {
        pos2 = pos1 + m - 1;
        if(s1[pos2] - s1[pos1 - 1] > tmp.first)
        {
            ans = pos1;
            tmp = {s1[pos2] - s1[pos1 - 1], s2[pos2] - s2[pos1 - 1]};
        }
        else if(s1[pos2] - s1[pos1 - 1] == tmp.first && s2[pos2] - s2[pos1 - 1] < tmp.second )
        {
            ans = pos1;
            tmp = {s1[pos2] - s1[pos1 - 1], s2[pos2] - s2[pos1 - 1]};
        }
        
    }
    cout << ans << endl;
    return 0;
}

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值