117482024-11-08 17:53:52balintÁruszállítás üres szakaszaicpp17Hibás válasz 2/5034ms512 KiB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Function to count the number of zero groups in the given integer
int count_zero_groups_from_int(unsigned int number, int bit_width) {
    int count = 0;
    int in_zero_group = 0;  // Flag to track if we're in a zero group

    // Iterate through the bits of the number from the most significant bit (MSB) to the least significant bit (LSB)
    for (int i = bit_width - 1; i >= 0; --i) {
        // Check if the i-th bit is 0
        if ((number & (1 << i)) == 0) {
            if (!in_zero_group) {
                count++;  // Found a new group of zeros
                in_zero_group = 1;  // We are now in a zero group
            }
        } else {
            // If the bit is 1, we're no longer in a zero group
            in_zero_group = 0;
        }
    }

    return count;
}

// Function to set bits in the given range [start, start + amount - 1]
void set_bits(unsigned int *bit_array, size_t start, size_t amount) {
    unsigned int mask = (1U << amount) - 1; // Create a mask with 'amount' number of 1s
    mask <<= start;  // Shift the mask to the 'start' position
    *bit_array |= mask;  // Set the bits using bitwise OR
}

// Function to print the bits of the integer in binary format
void print_bits(unsigned int *bit_array, int size) {
    for (int i = size - 1; i >= 0; --i) {
        printf("%d", (*bit_array >> i) & 1);  // Extract and print each bit
    }
    printf("\n");
}

int main() {
    unsigned int N, M;
    scanf("%u %u", &N, &M);  // Read the number of ranges and the bit width

    unsigned int sizebytes = (N + 7) / 8;  // Calculate the number of bytes to allocate for N bits
    unsigned int *bit_array = (unsigned int *)malloc(sizebytes);  // Allocate memory for the bit array
    memset(bit_array, 0, sizebytes);  // Initialize all bits to zero

    int from, to;
    for (unsigned int i = 0; i < M; i++) {  // Read M ranges and modify the bit array
        scanf("%d %d", &from, &to);  // Read the range (1-based indexing)
        set_bits(bit_array, from - 1, to - from);  // Set the bits from 'from' to 'to' (inclusive)
       // print_bits(bit_array, N);  // Print the bit array after modification
    }

    // Call the count_zero_groups_from_int function and print the result
    unsigned int number = *bit_array;  // Convert bit array to a single unsigned integer
    int zero_groups = count_zero_groups_from_int(number, N);
    printf("%d", zero_groups);  // Print the number of zero groups

    // Free the allocated memory for the bit array
    free(bit_array);

    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base2/50
1Elfogadva0/01ms320 KiB
2Hibás válasz0/034ms320 KiB
3Elfogadva2/21ms320 KiB
4Hibás válasz0/21ms320 KiB
5Hibás válasz0/21ms320 KiB
6Hibás válasz0/21ms320 KiB
7Hibás válasz0/21ms320 KiB
8Hibás válasz0/21ms320 KiB
9Hibás válasz0/21ms512 KiB
10Hibás válasz0/21ms324 KiB
11Hibás válasz0/21ms324 KiB
12Hibás válasz0/21ms320 KiB
13Hibás válasz0/33ms508 KiB
14Hibás válasz0/34ms408 KiB
15Hibás válasz0/33ms320 KiB
16Hibás válasz0/324ms504 KiB
17Hibás válasz0/324ms500 KiB
18Hibás válasz0/328ms508 KiB
19Hibás válasz0/34ms320 KiB
20Hibás válasz0/34ms320 KiB
21Hibás válasz0/330ms508 KiB
22Hibás válasz0/332ms508 KiB