Basics of Programming 1 solutions

Week 4 (10.03.)

Task descriptions are on the portal

Task 1

#include <stdio.h>

int main(void) {
    int counts[11] = {0};
    int result;
    while (scanf("%d", &result) == 1) {
        if (result < 11 && result > 0) {
            counts[result]++;
        }
    }
    for (int i = 1; i <= 10; i++) {
        printf("%d: %d pcs\n", i, counts[i]);
    }

    return 0;
}

Task 2

#include <stdio.h>

int main(void) {
    // SETUP
    bool sieve[1000];
    for (int i = 0; i < 1000; i++) {
        sieve[i] = true;
    }
    sieve[0] = sieve[1] = false;

    //CALC
    for (int i = 2; i < 1000; i++) {
        for (int j = 2 * i; j < 1000; j += i) {
            sieve[j] = false;
        }
    }

    //RESULT
    for (int i = 0; i < 1000; i++) {
        if (sieve[i]) printf("%d\n", i);
    }

    return 0;
}

Task 3

#include <stdio.h>

int main(void) {
    int month_lenghts[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int year, month, day;
    int total = 0;
    scanf("%d", &year);
    scanf("%d", &month);
    scanf("%d", &day);

    bool is_leap_year = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    for (int i = 0; i < month-1; i++) {
        total += month_lenghts[i];
    }
    if (month > 2 && is_leap_year) total++;
    total += day;

    printf("%d", total);

    return 0;
}