Editorial for Tích bốn số


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: MrTee

Bài này chúng ta đơn giản chỉ cần xem xem có số ~0~ hay không, nếu có thì in ra ~0~, ngược lại đếm xem có bao nhiêu số âm, nếu có lẻ số âm thì in ra ~-1~, chẵn thì in ra ~1~.

Chỉ có một lưu ý rằng, khi nhập dữ liệu nên nhập dưới dạng string, thay vì long double. Bởi nếu cho số ~0.00...001~ (có ~10^5~ số ~0~ sau dấu chấm), rất dễ sinh ra sai số.

Code C++

#include<bits/stdc++.h>
using namespace std;
#define ld long double
string s[5];
int check(string s) {
    for (int i=0; i<s.size(); i++) {
        if (s[i] == '-') continue;
        if (s[i] == '.') continue;
        if (s[i] != '0') return false;
    }
    return true;
}  
int main() {
    freopen("TBS.INP", "r", stdin);
    freopen("TBS.OUT", "w", stdout);
    for (int i=1; i<=4;i++) {
        cin >> s[i];
    }
    int l = 0;
    for (int i=1; i<=4; i++) {
        if (check(s[i])) {
            cout << 0;
            return 0;
        }
        if (s[i][0] == '-') l++;
    }
    if (l%2) cout << -1;
    else cout << 1;
}

Code python

import sys
sys.stdin = open("TBS.INP", "r")
sys.stdout = open("TBS.OUT", "w")

def is0(s):
    return s.count('0') + s.count('.') == len(s)

s = []

def main():
    for i in range(4):
        s.append(input())

    if len([x for x in s if is0(x)]):
        print(0)
        return 0

    if len([x for x in s if x[0] == '-']) & 1:
        print(-1)
        return 0

    print(1)
    return 0

if __name__ == '__main__':
    main()