任何人都可以帮我编程下一个问题吗(摘自Codingbat-Recursion1-count7)
给定非负
int n,
返回 occurrences of 7 as a digit
的计数,例如 717 yields 2.
(无循环)。请注意,mod (%)
除以 10 会得到 rightmost digit (126 % 10 is 6),
,而 (/) 除以 10 会删除最右边的数字(126 / 10 等于 12)。
count7(717) → 2
count7(7) → 1
count7(123) → 0
有一些解决方案包含“返回”的数量。 我想用只有 1 个“返回”的方法来解决这个问题。
public int count7(int n) {
int counter = 0;
if( n % 10 == 7) counter++;
if( n / 10 == 0) return counter;
return counter + count7(n/10);
}
当然 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) {
return (n==0?0:(count7(n/10)+(n%10==7?1:0)));
}
递归到 0 并返回 0,当返回时检查每个数字是否为 7,如果为 0,则返回 1。它会继续添加这些数字,直到全部结束。
使用一个回车符可能会增加阅读难度。如果您正在计算递归中的出现次数,一个简单的公式是创建一个要终止的基本情况,然后提供增量返回,最后提供一个有助于在不增加的情况下达到基本情况的返回。例如..
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);
}