Problem Statement A description of the problem can be found on Hackerrank. Solution Count all digits if the digit is greatest than 0 and if the input is divisible by digit. I created solution in: Java JavaScript Scala Ruby All solutions are also available on my GitHub. Java
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
|
import java.util.Scanner; public class FindDigits { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int tests = Integer.parseInt(stdin.nextLine()); for(int i = 0; i < tests; i++) { int result = 0; String number = stdin.nextLine(); for(char digit : number.toCharArray()) { if(Character.getNumericValue(digit) != 0 && Integer.parseInt(number) % Character.getNumericValue(digit) == 0) { result++; } } System.out.println(result); } stdin.close(); } } |
JavaScript
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
|
function processData(input) { var inputs = input.split('\n'); var tests = parseInt(inputs[0]); for(var i = 0; i < tests; i++) { for(var j = 1; j < inputs.length; j++) { var result = 0; var num = parseInt(inputs[j]); for(var k = 0; k < inputs[j].length; k++) { var c = parseInt(inputs[j][k]); if(c > 0 && num % c === 0) { result++; } } console.log(result); } return; } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); |
Scala
|
object FindDigits extends App { var inputs = io.Source.stdin.getLines().toList.tail inputs.foreach { in => println(in.toCharArray.count(d => d.getNumericValue != 0 && in.toInt % d.getNumericValue == 0)) } } |
Ruby
|
t = gets.to_i t.times do num = gets.chomp.to_s result = 0 num.split("").each do |c| c_num = c.to_i result +=1 if c_num > 0 && num.to_i % c_num == 0 end puts result end |