A number is called k -good if it contains the digits from 0 to k at least once. Given an…
- User
A number is called k -good if it contains the digits from 0 to k at least once.
Given an array a1,a2,…,an . Count the number of k -good numbers in the array.
Input The first line contains integers n and k (1≤n≤100 , 0≤k≤9 ). The i -th of the following n lines contains integer ai without leading zeroes (1≤ai≤109 ).
Output Print a single integer — the number of k -good numbers in a .
Examples inputCopy 10 6 1234560 1234560 1234560 1234560 1234560 1234560 1234560 1234560 1234560 1234560 outputCopy 10 inputCopy 2 1 1 10 outputCopy
- Solve it in c language
- Assistant
Here is a possible implementation in C language:
#include <stdio.h> #include <stdbool.h> bool isKGoodNumber(int num, int k) { bool digits[k+1]; for (int i = 0; i <= k; i++) { digits[i] = false; } while (num > 0) { digits[num % 10] = true; num /= 10; } for (int i = 0; i <= k; i++) { if (!digits[i]) { return false; } } return true; } int main() { int n, k; scanf("%d %d", &n, &k); int count = 0; for (int i = 0; i < n; i++) { int num; scanf("%d", &num); if (isKGoodNumber(num, k)) { count++; } } printf("%d\n", count); return 0; }The
isKGoodNumberfunction checks if a given number is a k-good number. It initializes an arraydigitsof sizek+1to false. Then, it iteratively sets the corresponding index in thedigitsarray to true for each digit in the given number. Finally, it checks if all digits from 0 to k have been encountered in the number.In the
mainfunction, we read the values ofnandkfrom input and iterate through the array of numbers. For each number, we use theisKGoodNumberfunction to determine if it is a k-good number. If it is, we increment the count. Finally, we print the count of k-good numbers.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.