Find The Digits || Codechef Solution

 

Find The Digits

Given an integer, , traverse its digits (1,2,...,n) and determine how many digits evenly divide  (i.e.: count the number of times  divided by each digit i has a remainder of ). 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 , the answer is ).

Input Format

The first line is an integer, , indicating the number of test cases.
The  subsequent lines each contain an integer, .

Constraints

Output Format

For every test case, count and print (on a new line) the number of digits in  that are able to evenly divide .


  • Solution:
  1. Python3.6

t=int(input())

for I in range(t):

    n=int(input())

    k=str(n)

    c=0

    for j in k:

        if(j=='0'):

            continue

        else:

            if(n%int(j)==0):

                c+=1 

    print(c)

  1. C++ Solution

#include<bits/stdc++.h>

#include<cstdlib>

using namespace std;

int main() {

    int n;

    cin>>n;

    while(n--){

        int a,cont=0;

        cin>>a;

        string b=to_string(a);

        for(int i=0;i<b.size();i++){

            if(b[i]=='0')continue;

            int dv=b[i]-'0';

            if(a%dv==0){

                cont++;

            }

        }

        cout<<cont<<endl;

    }

}

  1. Java Solution
import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Codechef obj = new Codechef();
Scanner scanner = new Scanner(System.in); 
int t = scanner.nextInt();
if(t>=1 && t<=15){
    for(int i=0;i<t;i++){
    int n = scanner.nextInt();
    int m=n;
    if(n>=0 && n<=1000000000){
        int count=0;
        while(n!=0){
            int r= n%10;
            if(r!=0 && m%r==0){
                count++;
            }
            n=n/10;
        }
        System.out.println(count);
    }
    }
}
}
}
🙏THANKS FOR VISIT🙏



Find The Digits || Codechef Solution Find The Digits || Codechef Solution Reviewed by CodexRitik on November 06, 2020 Rating: 5

No comments:

Powered by Blogger.