题解 | #游游的整数切割#
游游的整数切割
https://www.nowcoder.com/practice/c01b07fe9623425a806c85cdb6f0e0f7
假设s中有x个奇数数字(1、3、5、7、9),y个偶数数字。
若s的最后一个数字为奇数,那么切割后第一个数的最后一个数字也需要是奇数,答案为x-1(减去s最后的那个奇数,不能把所有数字都划到左边);偶数同理。
#include <iostream> #include <string> using namespace std; int main() { string s; cin >> s; int n = s.length(); int ans = 0; for (auto c : s) // if ((c - '0') % 2) ans++; if ((s[n - 1] - '0') % 2) { } else { ans = n - ans; } cout << ans - 1 << '\n'; }