חזור לתיקייה: מועד ב'

examB_q3

חיפוש בתיקיה
Ctrl K
לחץ כאן או הקש Ctrl K כדי לחפש בכל התיקיה

    examB_q3

    02340114

    מבוא למדמח מ

    רשימות הרצאה לקריאה בשבת

    examB_q3

    הופק מתוך תקיית התואר של שקד

    uni-folder.vercel.app

    examb-q3.c

    הערה (קובץ קוד)

    קובץ זה הוא קובץ קוד מסוג C.

    #include <stdbool.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define DONT_KNOW "I_dont_know"
    
    void printIDontKnow()
    {
        printf("%s", DONT_KNOW);
        exit(0);
    }
    
    int examB_q3(int num, int pattern);
    
    int main(void)
    {
        // uncomment next line if you don't know the answer
        // printIDontKnow();
    
        int num, pattern;
        if (scanf("%d %d", &num, &pattern) != 2)
            return 1;
    
        printf("%d", examB_q3(num, pattern));
        return 0;
    }
    
    #define NUMBER_OF_DIGITS 10
    
    // creates the histogram for the pattern
    void setUpPatternHist(int pattern, int *hist)
    {
        while (pattern > 0)
        {
            hist[pattern % 10]++;
            pattern /= 10;
        }
    }
    
    // checks if the start of a number is an anagram of a pattern
    bool isPattern(int num, int pattern)
    {
        int hist[9] = {0};
        setUpPatternHist(pattern, hist);
    
        while (pattern > 0)
        {
            if (hist[num % 10] > 0)
            {
                hist[num % 10]--;
            }
            else
            {
                return false;
            }
    
            pattern /= 10;
            num /= 10;
        }
    
        return true;
    }
    
    /*
     * This function checks how many times a sub number of <num> is an anagram
     * of a pattern.
     *
     * input:
     *  1. int num: the number to check
     *  2. int pattern: the pattern to check against
     * output(int):
     *  the number of sub numbers that are an anagram of the pattern.
    */
    int examB_q3(int num, int pattern)
    {
        int count = 0;
    
        while (num > 0)
        {
            if (isPattern(num, pattern))
            {
                count++;
            }
    
            num /= 10;
        }
    
        return count;
    }