首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我似乎无法使我的largestLength递归函数工作。

我似乎无法使我的largestLength递归函数工作。
EN

Stack Overflow用户
提问于 2014-02-09 16:01:54
回答 1查看 1.4K关注 0票数 0

无法使最大长度函数正确工作。

代码语言:javascript
复制
Everything else works perfectly. The function has to be recursive.
Assignment: 1a
Author:     Cory Church
File:       hailstone.cpp
Tab stops:  3

问题: Hailstone程序读取一个int,然后计算并打印该数字的冰雹序列。它还打印序列的长度,序列中的最大数,它们是多少个奇数,然后是从一个到读取的数的最长序列。

示例:

与方案的互动:

从7开始冰雹序列是7 22 11 34 17 52 52 26 13 40 10 16 8 4 2 1序列的长度为17.6。序列中最大的数目是52。最长的冰雹序列以7开头,长度为17。

代码语言:javascript
复制
#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 is\n", 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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-09 16:11:54

largestLength函数中有一个错误:

代码语言:javascript
复制
return max(length(n),largestLength(length(n-1)));

应该是

代码语言:javascript
复制
return max(length(n),largestLength(n-1));
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21661347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档