如何在c++中创建一个max函数来搜索数列中的最大值?



我以前在java中使用过数组,但是我在试图将其转换为与我当前的代码一起工作时遇到了麻烦。我仍然是c++的新手,我也在寻找一些评论。我的工作代码看起来像一个烂摊子,并希望建议如何使它看起来更整洁。我正在努力养成先写合同再写代码的习惯。(老实说,这帮助我更好地理解了自己在做什么)。我是说,它不干净。我想清洁它之后肯定,但我想尝试修复这部分。我只是想听听你的建议。我要在这方面多花点功夫。

// This program takes a user defined number 'n'
// and runs it through a function that returns 
// the number that follows 'n' in hailstone sequence.
// Since there is no number that follows 1 in the sequence,
// this function requires its parameter 'n' to be greater
// than 1. 
/* example output
What number shall I start with?  7
The hailstone sequence starting at 7 is:
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
The length of the sequence is 17.
The largest number in the sequence is 52.
The longest hailstone sequence starting with a number up to 7 has length 17
The longest hailstone sequence starting with a number up to 7 begins with 7
*/
#include <algorithm>
#include <cstdio>
using namespace std;
int main(int argc, char** argv)
{ 
    int n;
// hailLength will keep track of how many
// numbers are produced in the sequence.
    int hailLength = 1; 
    printf("What number shall I start with? ");
    scanf("%d", &n);
    printf("The hailtone sequence starting at %d is: n", n);
    printf("%i", n);
    printf(" ");
// While 'n' is not equal to 1 the function will calculate whether 'n' is even 
// then it will divide n/2, otherwise it will compute 3n + 1 if n is odd. 
    while(n != 1)
    {
        if(n % 2 == 0)
        {
            n /= 2;
            printf("%i", n);
            printf(" ");
        }
        else
        {
            n = (3 * n) + 1;
            printf("%i", n);
            printf(" ");
        }
        hailLength++;
    }
    printf("n");
    printf("The length of the sequence is %i.", hailLength);
    printf("n");
// This line will display the largest value in the hailstone sequence.
// This portion is also broken.
    int maximum(int n);
    {
        int k = 0;
        int ans = n;
        while(k != n)
        {
            k++;
            if(ans < k)
            {
                ans = k;
            }
        }
        //return ans;
        printf("%u", ans);
        //printf("%u", k);
        printf("n");
    }

//printf("The largest number in the sequence is %i", max(n));
return 0;
}

没有理由:

  1. 先生成列表

  2. 将该列表保存在某个地方,因为这是您的明显意图。

  3. 在生成整个列表并打印出来之后,返回然后计算出最大的数字本身是什么

这样做当然是可能的,它不是火箭科学,但它仍然是不必要的复杂。简单地跟踪看到的最大数字要简单得多……

int max;

…同时生成列表

就是这样。让我们开始:

scanf("%d", &n);
printf("The hailtone sequence starting at %d is: n", n);
max=n;

通过设置max为第一个数字开始滚动。很明显,这是迄今为止看到的最大的数字。

然后,在现有的for循环中,在计算出下一个数字并显示它之后,将其与max的当前值进行检查:

    }
    if (n > max)
         max=n;
    hailLength++;

然后,当循环结束时,您将找到序列中最大的数,在max中,已经。

您可以这样做来查找最大值。

#include <algorithm>    // std::max
int maxNumSoFar = n;
while( n != 1 ) {
    maxNumSoFar = std::max(n, maxNumSoFar);
    if( n%2 == 0) n /= 2;
    else n = 3*n+1
}
printf("Max number seen is %d", maxNumSoFar);

也因为你在c++中工作,而不是C,你可能想看看"cin"one_answers"cout"为你的I/O,而不是使用C的"printf"one_answers"scanf"。

最新更新