2014-02-09 59 views
0

無法獲得最大的長度函數來正常工作。我似乎無法得到我的最大長度遞歸函數的工作

Everything else works perfectly. The function has to be recursive. 
Assignment: 1a 
Author:  Cory Church 
File:  hailstone.cpp 
Tab stops: 3 

問題:該雹子程序在一個int讀取然後計算 並打印該號碼冰雹序列。它還 打印序列的長度,序列中數量最多, 多少奇數他們,然後從一個 在閱讀次數最長的序列

例:

與程序的交互:

我應該從哪開始? 7 從7開始的冰雹序列是序列的長度是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 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; 
} 
+1

是否存在特定問題,或者您是否期望我們調試代碼? – NPE

回答

2

largestLength函數有一個錯字:

return max(length(n),largestLength(length(n-1))); 

return max(length(n),largestLength(n-1)); 
相關問題