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;
}