如何在c++中创建一个max函数来搜索数列中的最大值?
How do I create a max function in c++ to search for the max in a sequence of numbers
我以前在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;
}
没有理由:
-
先生成列表
-
将该列表保存在某个地方,因为这是您的明显意图。
-
在生成整个列表并打印出来之后,返回然后计算出最大的数字本身是什么
这样做当然是可能的,它不是火箭科学,但它仍然是不必要的复杂。简单地跟踪看到的最大数字要简单得多……
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"answers"cout"为你的I/O,而不是使用C的"printf"answers"scanf"。
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- std::max() 函数与定点实现的比较中的问题
- 没有函数模板的实例"max"与参数列表参数类型匹配(int、int)
- 为什么在这种情况下不能将 Max 用作函数?
- 从递归向后选择排序函数调用 max 和交换函数
- 为什么 c++ 编译器在对两种不同类型的数值变量使用"std::max()"函数时会出现错误
- 自动变量可存储函数指针到std :: max
- 如何查找具有一组整数的函数的'max absolute sum'
- 在调试中,如何知道对函数的重复调用中参数的统计数据(max-min,average,distribution..)
- 当我尝试编译程序时,我遇到了一堆错误,例如:'std::max':找不到匹配的重载函数
- int max(一个变量)和 int max(int ,int)之间没有冲突,这是 C++ 中的一个函数
- c++ PPL 并行工作 - 归约类 'combinable' 中的函数 max()
- 在Armadillo稀疏矩阵中使用max函数时出错
- 如何将大于 std::numeric_limits<fundamental_type>::max() 的值传递给函数?
- 为template类定义numeric_limits-max函数
- 如何在c++中创建一个max函数来搜索数列中的最大值?
- 为数组创建c++ max函数:处理数组大小为0的情况的正确方法是什么?