Java 编码BAT-递归1-计数7

Java 编码BAT-递归1-计数7,java,Java,谁能帮我编程下一道题(摘自---) 给定一个非负的int n,返回出现7的计数作为一个数字,因此例如717产生2。(无循环)。请注意,mod(%)乘以10得到最右边的数字(126%10是6),而除以(/)10则删除最右边的数字(126/10是12) 有一些解决方案包括“退货”数量。 我只想用1个“return”来编程这个问题。当然,我的JAVA解决方案也是如此 public int count7(int n) { if((n / 10 == 0) && !(n % 10 =

谁能帮我编程下一道题(摘自---)

给定一个非负的
int n,
返回出现7的
计数作为一个数字,因此例如
717产生2。
(无循环)。请注意,
mod(%)
乘以10得到最右边的数字(126%10是6),
而除以(/)10则删除最右边的数字(126/10是12)

有一些解决方案包括“退货”数量。
我只想用1个“return”来编程这个问题。

当然,我的JAVA解决方案也是如此

public int count7(int n) {
   if((n / 10 == 0) && !(n % 10 == 7))      //First BASE CASE when the left most digit is 7 return 1
       return 0;     
   else if((n / 10 == 0) && (n % 10 == 7)) //Second BASE CASE when the left most digit is 7 return 0
        return 1;
   else if((n % 10 == 7))   
   //if the number having 2 digits then test the rightmost digit and trigger recursion trimming it there      
       return 1 + count7(n / 10);
   return count7(n / 10);
}
好吧,让我们看看如何做到这一点,只有一个回报

public int count7(int n) 
{
    int c = 0;
    if (7 > n)
    {
        return 0;
    }
    else
    {
        if ( 7 == n % 10)
        {
            c = 1;
        }
        else
        {
            c = 0;
        }
    }
    return c + count7(n / 10);  
}

public int count7(int n){
如果(n!=7&&n<10)返回0;
如果(n==7),则返回1;
如果(n%10==7)返回count7(n/10)+1;
其他返回计数7(n/10);
}
公共整数计数7(整数n){
if(n<7)
返回0;
否则如果(n%10==7)
返回1+count7(n/10);
其他的
返回计数7(n/10);
}
第一个if语句是我们希望终止的基本情况。第二个检查最右边的数字是否为7。如果是,请切掉最右边的数字,然后重试。当递归调用终止并且值开始在链上返回时,添加1以包括此成功检查。如果上述两种说法都不正确,请切掉最右边的数字,然后重试


我知道这已经是2年前的事了,但希望这更具可读性和直观性,从而对您有所帮助。

使用一次返回可能会使阅读更困难。如果要计算递归中出现的次数,一个简单的公式是创建一个要终止的基本情况,然后提供一个增量返回,最后是一个有助于在不增加的情况下达到基本情况的返回。例如

public int count7(int n) {
  int counter = 0;

  if( n % 10 == 7) counter++;

  if( n / 10  == 0)  return counter;

  return counter + count7(n/10); 
}
public int count7(int n) {
  if(n == 0) return 0;
  if(n % 10 == 7) return 1 + count7(n / 10);
  return count7(n / 10);
}
在我看来,使用像下面这样的一行返回值更难阅读或更新,因为它是双三元的

public int count7(int n) 
{
    return (n == 0) ? 0 : (n % 10 == 7) ? 1 + count7(n / 10) : count7(n / 10);  
}

我的解决方案通过取输入的模,从第n位向后工作到第一位。我们将找到的7的数量添加到返回中,作为最终输出

然后检查输入是否小于7可以是下一步。如果输入小于7,则输入中从未出现过任何7

public int count7(int n) {
        int sevens_found = 0;
        if( n % 10 == 7 ) sevens_found ++;
            return ( n < 7) ? 0 : ( n % 10 == 7 ) ? sevens_found + count7 ( n / 10 ) : count7 ( n / 10 );
        }
public int count7(int n){
int sevens_found=0;
如果(n%10==7)找到七个++;
返回值(n<7)?0:(n%10==7)?发现七个+计数7(n/10):计数7(n/10);
}
#包括
使用名称空间std;
整数计数\发生次数(整数k){
INTR;
如果(k==0){
返回0;
}
r=k%10;
k=k/10;
如果(r!=7){
返回计数(k);
}
返回1+计数发生次数(k);
}
int main()
{
int x;
cin>>x;

coutn==0的基本情况只是将我们从递归循环中“打断”,n%10==7允许我们在整数中实际计算7的数量,return语句迭代给定的参数

public int count7(int n) {
   if (n == 0) return 0;
   if (n % 10 == 7) return 1 + count7(n / 10);
   return count7(n / 10); 
}

codingBat的这个问题来自codingBat的JAVA部分,请将JAVA添加为标记。请发布到目前为止您拥有的所有代码。在学习网站的过程中询问完整答案会产生反效果。也许您可以添加一些解释?这样其他人也可以理解,而不仅仅是复制粘贴?嗨,欢迎来到stackoverflow。请详细描述答案。给出完整的答案将有助于其他人理解您的答案。我为我不完整的答案表示歉意。别担心,下次我将对我的答案添加更多解释。感谢您通知我。声明
count7(n%10)
会抓住数字7。你不需要第一个if语句来检查
n==7
。OP指定他们想要一个一次返回的解决方案,而你的有两次返回。按照问题的编写方式,我假设询问的人可能不知道你的解决方案为什么或如何工作。你能稍微扩展一下吗有什么解释吗?
public int count7(int n) {
  if (n != 7 && n < 10) return 0;
  else if (n == 7) return 1;
  else if (n%10 == 7) return count7(n/10) + 1 ;
  else return count7(n/10);
}
public int count7(int n){
    if(n < 7)
        return 0;
    else if(n % 10 == 7)
        return 1 + count7(n / 10);
    else
        return count7(n / 10);
}
public int count7(int n) {
  int counter = 0;

  if( n % 10 == 7) counter++;

  if( n / 10  == 0)  return counter;

  return counter + count7(n/10); 
}
public int count7(int n) {
  if(n == 0) return 0;
  if(n % 10 == 7) return 1 + count7(n / 10);
  return count7(n / 10);
}
public int count7(int n) 
{
    return (n == 0) ? 0 : (n % 10 == 7) ? 1 + count7(n / 10) : count7(n / 10);  
}
public int count7(int n) {
        int sevens_found = 0;
        if( n % 10 == 7 ) sevens_found ++;
            return ( n < 7) ? 0 : ( n % 10 == 7 ) ? sevens_found + count7 ( n / 10 ) : count7 ( n / 10 );
        }
#include<bits/stdc++.h>
using namespace std;

int count_occurences(int k){
    int r;
    if(k==0){
        return 0;
    }
    r = k%10;   
    k = k/10;

    if(r!=7){
        return count_occurences(k);
    }
    return 1+count_occurences(k);
    }



int main()
{
    int x;
    cin>>x;
      cout<<" "<<count_occurences(x);

return 0;
}
public int count7(int n) {
    int length = 0;
    int counter = 0;
    if ((n / 10) * 10 != n || (n / 10) != 0) {
        if (n % 10 != 7) {
            counter++;
        }
        length += 1 + count7(n / 10);
    }
    return length - counter;
}
public int count7(int n) {
   if (n == 0) return 0;
   if (n % 10 == 7) return 1 + count7(n / 10);
   return count7(n / 10); 
}