Problem:
Given a number like 1234, you need to find number of digits from number which divides the number.
Like 1234 when divided by 1,2,3,4->exact division is by digits 1,2 only, so output should be 2.
Input is in form of T, where T is number of test cases followed by numbers N
Constraints
1<=T<=15
0<N<10000000000
Sample Input:
3
121
123456789
12021
Sample Output:
2
3
2
Explanations:- T = 3, so 3 test cases. For 121, digits are 1,2,1->out of these only 1,1 exactly divides 121, so output is 2.
Solution:
chomp($t=<STDIN>);
if($t<1 or $t>15)
{
exit;
}
$tmp = 0;
@output = ();
for($i=0;$i<$t;$i++) #loop for each test case
{
chomp($a=<STDIN>);
if($a<=0 or $a>=10000000000)
{
exit;
}
@arr=split("",$a);
for($j=0;$j<@arr;$j++)
{
if($arr[$j] == 0) #skipping %0 cases
{
next;
}
elsif($a%$arr[$j] == 0) #checking for division
{
$tmp++;
}
}
push(@output,$tmp);
$tmp = 0;
}
foreach(@output)
{
print "$_\n";
}
Tips:
Take care of modulus with 0->it will give runtime error
Given a number like 1234, you need to find number of digits from number which divides the number.
Like 1234 when divided by 1,2,3,4->exact division is by digits 1,2 only, so output should be 2.
Input is in form of T, where T is number of test cases followed by numbers N
Constraints
1<=T<=15
0<N<10000000000
Sample Input:
3
121
123456789
12021
Sample Output:
2
3
2
Explanations:- T = 3, so 3 test cases. For 121, digits are 1,2,1->out of these only 1,1 exactly divides 121, so output is 2.
Solution:
chomp($t=<STDIN>);
if($t<1 or $t>15)
{
exit;
}
$tmp = 0;
@output = ();
for($i=0;$i<$t;$i++) #loop for each test case
{
chomp($a=<STDIN>);
if($a<=0 or $a>=10000000000)
{
exit;
}
@arr=split("",$a);
for($j=0;$j<@arr;$j++)
{
if($arr[$j] == 0) #skipping %0 cases
{
next;
}
elsif($a%$arr[$j] == 0) #checking for division
{
$tmp++;
}
}
push(@output,$tmp);
$tmp = 0;
}
foreach(@output)
{
print "$_\n";
}
Tips:
Take care of modulus with 0->it will give runtime error
No comments:
Post a Comment