标签:数位拆分、枚举、模拟
题意:给定区间 [ a , b ] [a,b] [a,b],求出区间内满足奇数位和等于偶数位和的数字个数
题解:遍历区间内每个数字,统计其奇数位和与偶数位和,如果相等,计数器加一,最后输出即可。
代码:
#include <bits/stdc++.h>
using namespace std;bool check(int x) {int sum1 = 0, sum2 = 0, p = 1;while (x) {if (p) sum1 += x % 10;else sum2 += x % 10;p ^= 1;x /= 10;}return sum1 == sum2;
}int main() {int a, b, ans = 0;cin >> a >> b;for (int i = a; i <= b; i++) {if (check(i)) ans++;}cout << ans << endl;return 0;
}