分析题目:
将输入的正整数n分割为n1和n2两部分(n1为前一部分,n2为后一部分)
无论如何分割,对于n2的个位数都会等于n的个位数
因此对于分割后n1+n2是否为偶数的判断
只需要考虑分割出来的n1的个位数!
因此从左往右遍历数组,每一个地方都分割一次判断是否为偶数
最后输出答案即可。
代码:
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl "\n"
#define PII pair<int,int>
#define PIII pair<int,PII>
const int MOD = 1e9 + 7;
const int N = 3e5;
bool cmp(PII p1, PII p2) {
return p1.first + p1.second < p2.first + p2.second;
}
bool isP(char t1, char t2) {
int c1 = t1 - '0';
int c2 = t2 - '0';
if ((c1 + c2) & 1)return false;
return true;
}
void slu() {
string t;
cin >> t;
int cnt = 0;
for (int i = 0; i < t.size() - 1; i++) {
if (isP(t[i], t[t.size() - 1]))cnt++;
}
cout << cnt;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
// cin >> T;
T = 1;
while (T--)slu();
}

京公网安备 11010502036488号