HACKER RANK Algorithms - Implementation - Find Digits - Solution.'C++'
Algorithms - Implementation - Find Digits - Solution
Problem Statement
Given an integer, N , traverse its digits (d 1,d 2,...,d n) and determine how many digits evenly divide N (i.e.: count the number of times N divided by each digit d i has a remainder of 0 ). Print the number of evenly divisible digits.
Note: Each digit is considered to be unique, so each occurrence of the same evenly divisible digit should be counted (i.e.: for N=111 , the answer is 3 ).
Input Format
The first line is an integer, T , indicating the number of test cases.
TheT subsequent lines each contain an integer, N .
The
Constraints
1≤T≤15
0<N<1010
Output Format
For every test case, count and print (on a new line) the number of digits in N that are able to evenly divide N .
Sample Input
2
12
1012
Sample Output
2
3
Explanation
The number 12 is broken into two digits, 1 and 2 . When 12 is divided by either of those digits, the calculation's remainder is 0 ; thus, the number of evenly-divisible digits in 12 is 2 .
The number 1012 is broken into four digits, 1 , 0 , 1 , and 2 . 1012 is evenly divisible by its digits1 , 1 , and 2 , but it is not divisible by 0 as division by zero is undefined; thus, our count of evenly divisible digits is 3 .
This challenge was part of Pragyan 12.
Solution:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int t, m, i=0, num, a, count=0;
cin>>t;
while(t--)
{
cin>>num;
m=num;
while(num)
{ a=num%10;
num=num/10;
if(a!=0)
if(m%a==0)
count++;
}
cout<<count<<endl;
count=0;
}
return 0;
}
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int t, m, i=0, num, a, count=0;
cin>>t;
while(t--)
{
cin>>num;
m=num;
while(num)
{ a=num%10;
num=num/10;
if(a!=0)
if(m%a==0)
count++;
}
cout<<count<<endl;
count=0;
}
return 0;
}
Comments
Post a Comment