[Mảng 1 Chiều Cơ Bản]. Bài 1. Chẵn lẻ

View as PDF

Submit solution

Points: 1.00 (partial)
Time limit: 1.0s
Memory limit: 256M
Input: stdin
Output: stdout

Problem source:
28Tech
Problem type
Allowed languages
C, C#, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

Cho mảng số nguyên A[] gồm N phần tử, nhiệm vụ của bạn là đếm xem trong mảng có bao nhiêu số chẵn, bao nhiêu số lẻ, tổng các phần tử là số chẵn, tổng các phần tử là số lẻ.


Đầu vào

Dòng đầu tiên là số nguyên dương N

Dòng thứ 2 gồm N số nguyên viết cách nhau một vài khoảng trắng


Giới hạn

1<=N<=1000

-10^3<=A[i]<=10^3


Đầu ra

Dòng đầu tiên in ra số lượng số chắn.

Dòng thứ 2 in ra số lượng số lẻ.

Dòng thứ 3 in ra tổng các số chẵn.

Dòng thứ 4 in ra tổng các số lẻ.


Ví dụ :

Input 01
6
-711 327 372 779 451 -864
Output 01
2
4
-492
846

Comments

Please read the guidelines before commenting.



  • -2
    peterphuc290306  commented on Oct. 30, 2024, 4:10 a.m.

    include <stdio.h>

    include <string.h>

    include <math.h>

    include <stdlib.h>

    int chan(int a[], int n){ int dem = 0; for(int i = 0; i < n; i++){ if(a[i] % 2 == 0) ++dem; } return dem; }

    int le(int a[], int n){ int dem = 0; for(int i = 0; i < n; i++){ if(a[i] % 2 != 0) ++dem; } return dem; }

    int tongchan(int a[], int n){ int s = 0; for(int i = 0; i < n; i++){ if(a[i] % 2 == 0) s += a[i]; } return s; }

    int tongle(int a[], int n){ int s = 0; for(int i = 0; i < n; i++){ if(a[i] % 2 != 0) s += a[i]; } return s; }

    int main() { int n; scanf("%d", &n); int a[n]; for(int i = 0; i < n; i++){ scanf("%d", &a[i]); } printf("%d", chan(a, n)); printf("\n%d", le(a, n)); printf("\n%d", tongchan(a, n)); printf("\n%d", tongle(a, n)); return 0; }