checking an integer to see if it contains a zero

前端 未结 7 1788
南方客
南方客 2020-12-14 21:09

Given an integer, how could you check if it contains a 0, using Java?

1 = Good
2 = Good
...
9 = Good
10 = BAD!
101 = BAD!
1026 = BAD!
1111 = Good

How c

7条回答
  •  生来不讨喜
    2020-12-14 21:28

    Not using Java, but it's not exactly hard to convert from C++ PS. Shame on anyone using string conversion.

    bool Contains0InBase10( unsigned int i, unsigned int& next )
    {
     unsigned int divisor = 10;
     unsigned int remainder = 0;
     while( divisor <= i )
     {
      unsigned int newRemainder = i%divisor;
      if( newRemainder - remainder == 0)
      {
       // give back information allowing a program to skip closer to the next
       // number that doesn't contain 0
       next = i + (divisor / 10) - remainder;
       return true;
      }
      divisor *= 10;
      remainder = newRemainder;
     }
     return false;
    }
    

提交回复
热议问题