forked from Silver-Taurus/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
findDigits.cpp
59 lines (55 loc) · 1.31 KB
/
findDigits.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* You are given an integer N. Find the digits in this number that exactly divide N (division that leaves 0 as remainder) and display their count.
* For N=24, there are 2 digits (2 & 4). Both of these digits exactly divide 24. So our answer is 2.
*
* Note
*
* If the same number is repeated twice at different positions, it should be counted twice,
* e.g., For N=122, 2 divides 122 exactly and occurs at ones' and tens' position. So for this case, our answer is 3.
* Division by 0 is undefined.
*
*
* Input Format
* The first line contains T (the number of test cases), followed by T lines (each containing an integer N).
*
* Constraints
* 1≤T≤15
* 0<N<1010
* Output Format
*
* For each test case, display the count of digits in N that exactly divide N in a separate line.
*
* Sample Input
*
* 2
* 12
* 1012
*
* Sample Output
* 2
* 3
*/
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int T, num;
cin >> T;
for (int t = 0; t < T; ++t) {
cin >> num;
int c = 0, d =0;
int x = num;
while(x > 0) {
d = x % 10;
if ( d != 0 && num % d == 0) {
++c;
}
x = x/10;
}
cout << c << endl;
}
return 0;
}