string
类型读入整数,直接操作字符串,最后还可以用 stoi
函数自动去掉有前导0的情况。#include <iostream>
using namespace std;
int main()
{
string str;
cin >> str;
for (auto& e : str)
{
if (e % 2) e = '1';
else e = '0';
}
cout << stoi(str) << endl;
return 0;
}
#include <iostream>
using namespace std;
const int N = 1e6 + 1;
using ll = long long;
ll row[N], col[N];
ll n, m;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
ll arr[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> arr[i][j];
row[i] += arr[i][j];
col[j] += arr[i][j];
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << row[i] + col[j] - arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
const int N = 1e5 + 1;
using ll = long long;
ll h[N], s[N];
ll sumh, sums, n, k;
int main()
{
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> h[i];
for (int i = 1; i <= n; i++) cin >> s[i];
ll maxh = 0, mins = 0, day = 0;
for (int l = 1, r = 1; r <= n; r++)
{
sumh += h[r];
sums += s[r];
if (r - l + 1 == k)
{
if (sumh > maxh)
{
maxh = sumh;
mins = sums;
day = l;
}
else if (sumh == maxh && sums < mins)
{
maxh = sumh;
mins = sums;
day = l;
}
sumh -= h[l];
sums -= s[l++];
}
}
cout << day << endl;
return 0;
}
#include <iostream>
using namespace std;
const int N = 1e5 + 1;
using ll = long long;
ll preh[N], pres[N];
ll sumh, sums, n, k;
int main()
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
int a; cin >> a;
preh[i] = preh[i - 1] + a;
}
for (int i = 1; i <= n; i++)
{
int b; cin >> b;
pres[i] = pres[i - 1] + b;
}
ll maxh = 0, mins = 0, day = 0;
for (int i = k; i <= n; i++)
{
if (preh[i] - preh[i - k] > maxh)
{
maxh = preh[i] - preh[i - k];
mins = pres[i] - pres[i - k];
day = i - k + 1;
}
else if (preh[i] - preh[i - k] == maxh)
{
if (pres[i] - pres[i - k] < mins)
{
maxh = preh[i] - preh[i - k];
mins = pres[i] - pres[i - k];
day = i - k + 1;
}
}
}
cout << day << endl;
return 0;
}
本篇文章的分享就到这里了,如果您觉得在本文有所收获,还请留下您的三连支持哦~
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有