0

In this question what I have to do is: find the no. of digits in the given string which are divisor of the complete number represented by the string.

For numbers without 0 in it it is working fine. for example t=1 , s="12" , output=2

#include<bits/stdc++.h>
using namespace std;

int main()
{
    string s;
    long long t,i,n,ct;
    cin>>t;

    while(t--)
    {
        ct=0;
        cin>>s;
        n=stoi(s);

        for(i = 0; i < s.length(); i++)
        {
            if(n%(s[i]-'0') == 0 && s[i] != '0')
            {
                ct++;
            }
        }

        cout<<ct<<endl;
    }
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
  • what is the code supposed to do? What is "working fine" and what is "not working" ? – 463035818_is_not_an_ai Nov 22 '20 at 21:07
  • Have you actually tried to debug your own code? Set breakpoints and go step by step? – NRUB Nov 22 '20 at 21:14
  • Related: https://stackoverflow.com/questions/7370154/cant-mod-zero – Lukas-T Nov 22 '20 at 21:14
  • Also don't write `#include` and don't use `using namespace std;`. Read about it here, https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice and https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H. – NRUB Nov 22 '20 at 21:17
  • hi , this question is from hackerrank - [link](https://www.hackerrank.com/challenges/find-digits/problem) . The input is integer i know but for practicing and my curiosity purpose only i tried to do it using string. And yes i always debug my code using print statements also. What is not working in this code is if the string is a number having zero in it then `the code stops working on encountering 0 otherwise for the numbers which don't have zero in it , it works fine. – kapil rajoria Nov 23 '20 at 15:10

1 Answers1

3
if(n%(s[i]-'0') == 0 && s[i] != '0')

For a start, this is in the wrong order. You want to ensure it's not '0' before attempting a division-type operation. The reason for this is that C++ uses short-circuiting operation so that in the following code, if xyzzy is false, plugh will never be evaluated:

if (xyzzy && plugh)

If xyzzy is a "dangerous" operation that shouldn't happen when plugh is false, you'll get into trouble.

In your specific code, what you have will first try to evaluate the division before checking the digit. So, it will allow division by zero to be performed, not really a good idea. It would be safer to use:

if ((s[i] != '0') && (n % (s[i] - '0') == 0))

There's a few other minor issues in your code such as signed/unsigned comparisons, or the not-really-necessary use of long long(a), or not following the guideline of declaring variables where they're needed rather than at some point long before then (such as at the top of the function).

But it's the order of the sub-expressions in the if statement that's most likely your immediate issue.


(a) Just how long do you expect these strings to get? :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Thank you so much!!!! Now it's working fine. I will really take care of the points you told next time. Btw in the original question the input was integer and can be between 0 to 10^9. – kapil rajoria Nov 23 '20 at 15:25