【題解】Codeforces 1291A. Even But Not Even

【題目敘述】https://codeforces.com/contest/1291/problem/A
【解題想法】

  • 題目強調:It’s not necessary to minimize or maximize the number of deleted digits.
  • 如果在字串 s 中可以找到兩個奇數,即可滿足Even But Not Even。
#include <iostream>
using namespace std;
 
int t, n, odd1, odd2;
string s;
 
int main() {
    cin >> t;
    while (t--){
        cin >> n;
        cin >> s;
        odd1 = -1;
        odd2 = -1;
        for (int i = 0; i < n; i++){
            if ((s[i]-'0') % 2 == 1){
                odd1 = odd2;
                odd2 = (s[i]-'0');
                if (odd1 > 0) break;
            }
        }
        if (odd1 > 0) cout << odd1 << odd2 << "\n";
        else cout << "-1\n";
    }
}
分享本文 Share with friends