Editorial for Đếm số âm dương
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.
Submitting an official solution before solving the problem yourself is a bannable offence.
Spoiler Alert
Hint 1
- Duyệt qua và đếm
Khởi tạo \(posi\) là số lượng số âm
Khới tạo \(nega\) là số lượng số dương
Mình duyệt qua \(n\) phần tử \(x\)
Tăng giá trị \(posi\) khi \(x > 0\)
Tăng giá trị \(nega\) khi \(x < 0\)
Sau đó xuất kết quả \(nega\) " " \(posi\)
Reference AC code | \(O(n)\) time | \(O(1)\) auxiliary space | Online Solving, Implementation
C++
int main()
{
int posi = 0, nega = 0;
for (int n = readInt(); n--; )
{
int x = readInt();
posi += (x > 0);
nega += (x < 0);
}
cout << nega << ' ' << posi;
return 0;
}
Comments