开发者

Count the number of '8's Problem in Java

Im stuck with the below problem.

Problem Statement:

Given a non-negative int n, return the count of the occurrences of 8 as a digit, except that an 8 with another 8 immediately to its left counts double, so 8818 yields 4.

Note: mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).

The above problem has to be solved without using Recursion and without the usage of any formulas.

The function signature is public int count8(int n)

Examples are:

count8(8) → 1     
count8(818) → 2     
count8(8818) → 4

I got this 开发者_开发技巧problem from one of the Programming Forums. I dont know how to start with this problem, I want to solve it, but I am really confused on where to begin.


the way to do this using the mod operator is to use %10 to get the last digit and /10 to remove the last digit in essence iterating through the number. If you %10 and get an 8 you can incremement a count, you can also keep a flag that lets you know if the last digit you saw was an 8 or not so you know how to increment your count

boolean lastWas8 = false;
int count = 0;
while (n != 0)
{
    int digit = n % 10;
    if (digit == 8)
    {
        if (lastWas8) count++;
        count++;
        lastWas8 = true;
    }
    else lastWas8 = false;
    n/=10;
}
return count;


As none of the answers until now was recursive, here is my try at a recursive solution.

public int count8(int n) {
  return
     n <= 0 ? 0 :
     ( n%100 == 88 ? 2 :
       n%10 == 8 ? 1 : 0)
     + count8(n/10);
}

Here the same program in a longer version:

public int count8(int n) {

Numbers without digits have no eights in them.

    if(n <= 0) {
       return 0;
    }

Count the last digit:

    int last;

If the last digit is an 8 and the digit before, too, count the last 8 doubled:

    if(n % 100 == 88) {
        last = 2;
    }

If the last digit is an 8 (and the one before not), count it once.

    else if(n % 10 == 8) {
        last = 1;
    }

Otherwise, the last digit is not an 8:

    else {
         last = 0;
    }

The number without the last digit:

   int withoutLast = n/10;

The number of eights in n is the number of eights in the last digit + the number of eights in the number without its last digit:

    return last + count8(withoutLast);
}

Since I misread the question, here a iterative version of the same algorithm:

public int count8(int n) {
    int count = 0;
    while(n > 0) {
       count += ( n%100 == 88 ? 2 : n%10 == 8 ? 1 : 0);
       n/= 10; 
    }
    return count;
}

Or with a for-loop:

public int count8(int n) {
    int count = 0;
    for( ; n > 0; n/=10) {
       count += ( n%100 == 88 ? 2 : n%10 == 8 ? 1 : 0);
    }
    return count;
}


I saw that all the other solutions have used mods or divs but you could also just process it as a String I guess (I don't see anything in the question that says you can't despite the hints they give you). This is just an alternative solution.

I apologise in advance if I have missed some of the "rules" around the answer to this question but here we go anyway:

private int count8(int n) {
    String nString = Integer.toString(n);
    boolean isPrevChar8 = false;
    int total = 0;

    for (int i = 0; i < nString.length(); i++) {
        char nextChar = nString.charAt(i);

        if (nextChar == '8') {
            total += (isPrevChar8 ? 2 : 1);
            isPrevChar8 = true;
        } else {
            isPrevChar8 = false;
        }
    }
    return total;
}


try this :

public static int count8(int num) {
        int count=0;
        boolean doubl = false;
        while(true) {
            int n = num%10;
            num = num/10;

            if(n==8) {

                if(doubl) {
                    count = count+2;
                } else {
                    count++;
                }
                doubl=true;
            }
            else {
                doubl=false;
            }
            if(num == 0) break;
        }
        return count;
    }

EDIT: Check this out for no recursion and no formula.

 public static int count8(int num) {
        int count=0;
        boolean doubl = false;

        String str = "" + num;

        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == '8') {
                if (doubl) {
                    count = count + 2;
                } else {
                    count++;
                }
                doubl = true;
            } else {
                doubl = false;
            }
        }
        return count;
    }


Here is my solution:

 public int count8(int n) {

   int count = 0;

   if(n == 0)
       return 0;

   if(n % 100 == 88)
   {
       count = 3;
       return count + count8(n/100);
   }

   else if(n % 10 == 8)
   {
     count++;
     return count + count8(n/10); 
   }

   else
       return count8(n/10);
   }

However, for the case: count8(88888) → 9, I get 7, and I can't figure out why. What I also find strange is that a double 8 yields 3 so for the case: count8(8818) → 4 instead of 5, which is what I thought it would be. Hence, why I have count = 3 for the (n % 100 == 88)


Here is my code . The solution to this problem is very simple . I have done it with pure recursion . :)

public int count8(int n) {
       if (n==8) return 1; 
       if (n<10) return 0;
       if (n%100==88) 
       return 2 + count8(n/10);
       if (n%10==8)
       return 1 + count8(n/10);
       return count8(n/10);
}

The catch of the problem is that when a pair of 88 comes total count = 1 + 2 ; 1 for 8 at right and 2 for 8 at left because the previous digit(which is digit at its adjacent right) was also 8 .

So for 88 the total occurances of 8 is equal to 3. For implementing this logic (n%100 ==88) condition is added .


This is the another recursion technique which I have used to solve this problem :-

public int count8(int n) {
int a,b;
if(n==0)
return 0;
a=n%10;
b=(n/10)%10;
if(a==8&&b==8)
return 2+count8(n/10);
else if(a==8&&b!=8)
return 1+count8(n/10);
else
return count8(n/10);

}


This code also works;

public int count8(int n) {
  if(n/10 == 0 && n%10 != 8){
    return 0;
  }
  if(n % 10 == 8 && (n/10)%10 == 8){
    return 2 + count8(n/10);
  }
  if(n/10 == 0 && n%10 == 8){
    return 1 + count8(n/10);
  }
  if(n % 10 != 8){
    return 0 + count8(n/10);
  }else{
    return 1 + count8(n/10);
  }
 }


Here is simple solution

public int count8(int n) {
  //base case if n becomes 0 then return 0
  if(n==0) return 0;

  //checking for two consecutive 8's in a row
  if((n%10) == 8 && (n/10)%10 == 8){
    return 2 + count8(n/10);
  }
  else if(n%10 == 8){ // there is only one 8 
    return 1 + count8(n/10);
  }
  //no 8 found 
  return count8(n/10);
}


Here's my solution, albeit the function names aren't nicely named, just think of them as abstract (not in the Java abstract keyword sense) functions that perform their task.

public int count8(int n) {
    return g(n, 0); 
}

public int g(int n, int prev) {
    int rest = n/10;
    int digit = n % 10;
    if (rest == 0) {
       return h(digit, prev);
    }

    int toAdd = h(digit, prev);
    return toAdd + g(rest, digit);
}


    public int h(int digit, int prev) {
        return prev == 8 && digit == 8 ? 
                                       2 : digit == 8 ? 
                                                      1 : 0;    
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜