我正在向Recursion-1部分下的Codingbat寻求编程挑战方面的帮助,特别是count7问题。任务是使用不带循环的递归并将函数限制为单个
n
语句,计算给定非负整数 return
中数字“7”的出现次数。
例如:
count7(717)
应该返回2
。count7(7)
应该返回1
。count7(123)
应该返回0
。详情:
%
) 除以 10 会得到最右边的数字(例如,126 % 10
结果为 6
)。/
) 除以 10 会删除最右边的数字(例如,126 / 10
变为 12
)。我的目标是找到一种解决方案,将这些操作合并到只有一个 return 语句的递归函数中。有人可以帮忙吗?
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) {
return (n == 0 ? 0 : (count7(n / 10) + (n % 10 == 7 ? 1 : 0)));
}
n
是 0
,则函数立即返回 0
,因为没有剩余数字可供分析。n / 10
调用自身,有效地处理没有最后一位数字的数字。如果当前 1
(n
) 的最后一位数字是 n % 10
,则添加 7
,否则添加 0
。(count7(n / 10))
,计算除最后一位之外的所有数字中的7
,然后使用(n % 10 == 7 ? 1 : 0)
单独计算最后一位数字。这可以有效地利用带有单个
return
语句的递归来计算给定整数中数字 7
的出现次数。
当然 PFB 我在 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) {
if(n == 0)
return 0;
else{
if(n%10 ==7)
return 1+count7(n/10);
return 0+count7(n/10);
}
}
public static int count7(int n){
if(n == 7)
return 1;
else if(n > 9){
int a = count7(n%10);
int b = count7(n/10);
return a + b;
}else
return 0;
}
public int count7(int n)
{
if(n==0)return 0;
if(n%10==7)return 1+count7(n/10);
else return count7(n/10);
}
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);
}
第一个 if 语句是我们想要终止的基本情况。第二个检查最右边的数字是否是 7。如果是,则截掉最右边的数字并重试。当递归调用终止并且值开始在链上返回时,加 1 以包含此成功的检查。如果以上说法都不成立,请砍掉最右边的数字,然后重试。
我知道这是 2 年前的文章,但希望它更具可读性和直观性,从而有所帮助。
使用一个回车符可能会增加阅读难度。如果您正在计算递归中的出现次数,一个简单的公式是创建一个要终止的基本情况,然后提供增量返回,最后提供一个有助于在不增加的情况下达到基本情况的返回。例如..
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。
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;
}
n == 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);
}
public int count7(int n)
{
return occurrencesCounting(n, 0);
}
private int occurrencesCounting(int n, int count)
{
int counter = n % 10 == 7 ? count + 1 : count;
if (n / 10 == 0)
{
return counter;
}
return occurrencesCounting(n / 10, counter );
}
public int count7(int n) {
if(n<7)return 0;
if(n%10==7)return 1+count7(n/10);
else return count7(n/10);
}