我似乎无法让我的最大长度递归函数工作
I can not seem to get my largestLength recursive function to work
无法使最大的长度函数正常工作。
Everything else works perfectly. The function has to be recursive.
Assignment: 1a
Author: Cory Church
File: hailstone.cpp
Tab stops: 3
问题:冰雹程序读取整数然后计算并打印该数字的冰雹序列。它还打印序列的长度,序列中的最大数字,它们是多少个奇数,然后是从一个中最长的序列到读入的数字。
例:
与程序的交互:
我应该从什么数字开始? 7从 7 开始的冰雹序列是7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1序列的长度为 17。其中6个数字是奇数。序列中的最大数字是 52。最长的冰雹序列以最多 7 的数字开头,长度为 17
#include <cstdio>
using namespace std;
#include <algorithm>
// ****************************************************************************
/* next(n) returns the value of the next value in the hailstone
sequence when the current value is n.
Example: next(7) = 22 and next(22) = 11.
*/
int next(int n)
{
int i = n;
if (i % 2 == 0)
{
i = i/2;
}
else
{
i = 3 * i + 1;
}
return (i);
}
// ****************************************************************************
/* printHailstoneSequence(n) writes out all of the values of the hailstone
sequence starting with n.
Example: printHailstoneSequence(7);
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
*/
void printHailstoneSequence(int n)
{
int hail = n;
if (hail == 1)
{
printf("%i ", hail);
}
else
{
printf("%i ", hail);
printHailstoneSequence(next(hail));
}
}
// ****************************************************************************
// length(n) returns the length of the Hailstone sequence of n.
// Example: length(7) = 17
int length(int n)
{
int hail = n;
if(hail == 1)
{
return 1;
}
else
{
return 1+length(next(hail));
}
}
// ****************************************************************************
/* largest(n) determines the largest number of the Hailstone sequence
starting with n.
Example: largest(7)= 52
*/
int largest(int n)
{
int hail = n;
if (hail == 1)
{
return 1;
}
else
{
return max(largest(next(hail)), hail);
}
}
// ****************************************************************************
/* odd(n) determines the number of odd numbers in the Hailstone sequence
starting with n.
Example: odd(7)= 6
*/
int odd(int n)
{
if (n==1)
{
return 1;
}
else if (n%2 == 0)
{
return odd(next(n));
}
else
{
return 1+ odd(next(n));
}
}
// ****************************************************************************
/* largestLength(n) determines the longest length of any Hailstone sequence
from 1 to n.
Example: largestLength(7)= 17
*/
int largestLength(int n)
{
if (n == 1)
{
return 1;
}
else
{
return max(length(n),largestLength(length(n-1)));
}
}
// ****************************************************************************
int main(int argc, char** argv)
{
int n;
printf("What number should I start with? ");
scanf("%i", &n);
printf("The hailstone sequence starting at %i isn", n);
printHailstoneSequence(n);
printf("n");
printf("The length of the sequence is %i. n", length(n));
printf("%i of the numbers are odd. n", odd(n));
printf("The largest number in the seguence is %i. n", largest(n));
printf("The longest hailstone sequence starting with a number up to %i has the length %i n", n, largestLength(n));
return 0;
}
在largestLength
函数中有一个拼写错误:
return max(length(n),largestLength(length(n-1)));
应该是
return max(length(n),largestLength(n-1));
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何在Elixir中调用递归函数并行
- 递归函数有效,但无法记忆
- 为什么我的递归函数按降序打印,然后按升序打印?
- 为什么递归函数的最终输出是 5?
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- 如何将记忆应用于此递归函数?
- 如何从递归函数中完全返回,该函数给出了每个函数结果的累积相加?
- 无穷大而循环时具有递归函数
- 即使没有调用这个递归函数,它是如何工作的?
- 如何使此递归函数从给定的起始位置返回最小的整数?
- 此递归函数的每次迭代的值存储在哪里?
- 可以清除递归函数中的变量吗?
- 递归函数调用在后台工作
- 这个递归函数是如何工作的?
- 我对变量在递归函数中如何工作的理解是否正确?
- 我似乎无法让我的最大长度递归函数工作
- 模板作为递归函数工作
- 二叉搜索树 序横向递归函数无法正常工作
- 不明白为什么这个C++递归函数工作背后的逻辑